-
Notifications
You must be signed in to change notification settings - Fork 0
/
calendar.php
134 lines (109 loc) · 4.5 KB
/
calendar.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
<?php
session_start(); // Start session if using user-specific IDs
// Include database connection
include 'db.php';
// Set current month and year
$month = isset($_GET['month']) ? (int)$_GET['month'] : date('m');
$year = isset($_GET['year']) ? (int)$_GET['year'] : date('Y');
// Function to display calendar days
function buildCalendar($month, $year) {
$daysOfWeek = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
$firstDayOfMonth = mktime(0, 0, 0, $month, 1, $year);
$numberDays = date('t', $firstDayOfMonth);
$dateComponents = getdate($firstDayOfMonth);
$dayOfWeek = $dateComponents['wday'];
// Get today's date for comparison
$today = date('Y-m-d');
$calendar = "<table class='calendar'>";
$calendar .= "<tr class='header'>";
foreach ($daysOfWeek as $day) {
$calendar .= "<th>$day</th>";
}
$calendar .= "</tr><tr>";
// Fill in the blanks for days before the first day of the month
if ($dayOfWeek > 0) {
$calendar .= str_repeat('<td></td>', $dayOfWeek);
}
$currentDay = 1;
while ($currentDay <= $numberDays) {
if ($dayOfWeek == 7) {
$dayOfWeek = 0;
$calendar .= "</tr><tr>";
}
// Format date string for the current day
$dateString = sprintf('%04d-%02d-%02d', $year, $month, $currentDay);
// Check if the current date matches today's date
$isToday = ($dateString == $today) ? 'today' : '';
$calendar .= "<td class='$isToday'><span class='date' data-date='$dateString'>$currentDay</span></td>";
$currentDay++;
$dayOfWeek++;
}
if ($dayOfWeek != 7) {
$remainingDays = 7 - $dayOfWeek;
$calendar .= str_repeat('<td></td>', $remainingDays);
}
$calendar .= "</tr></table>";
return $calendar;
}
// Save reminder
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$user_id = $_SESSION['user_id']; // Assuming user ID is in session
$reminder_date = $_POST['reminder_date'];
$reminder_text = $_POST['reminder_text'];
$stmt = $conn->prepare("INSERT INTO reminders (user_id, reminder_date, reminder_text) VALUES (?, ?, ?)");
$stmt->bind_param("iss", $user_id, $reminder_date, $reminder_text);
$stmt->execute();
$stmt->close();
}
// Fetch reminders
function fetchReminders($user_id) {
global $conn;
$stmt = $conn->prepare("SELECT reminder_date, reminder_text FROM reminders WHERE user_id = ? ORDER BY reminder_date ASC");
$stmt->bind_param("i", $user_id);
$stmt->execute();
$result = $stmt->get_result();
$reminders = $result->fetch_all(MYSQLI_ASSOC);
$stmt->close();
return $reminders;
}
$reminders = fetchReminders($_SESSION['user_id']); // Get reminders for logged-in user
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Interactive Calendar</title>
<link rel="stylesheet" href="css/calendar.css">
</head>
<body>
<div class="calendar-container">
<h2>Calendar - <?php echo date('F Y', strtotime("$year-$month-01")); ?></h2>
<div class="month-nav">
<a href="?month=<?php echo $month - 1; ?>&year=<?php echo $year; ?>">Prev</a>
<a href="?month=<?php echo $month + 1; ?>&year=<?php echo $year; ?>">Next</a>
</div>
<?php echo buildCalendar($month, $year); ?>
<div class="reminder-form">
<h3>Set Reminder</h3>
<form method="POST">
<label for="reminder_date">Date:</label>
<input type="date" id="reminder_date" name="reminder_date" required>
<label for="reminder_text">Reminder:</label>
<input type="text" id="reminder_text" name="reminder_text" required>
<button type="submit">Add Reminder</button>
</form>
</div>
<div class="reminder-list">
<h3>Your Reminders</h3>
<ul>
<?php foreach ($reminders as $reminder): ?>
<li><?php echo $reminder['reminder_date']; ?> - <?php echo $reminder['reminder_text']; ?></li>
<?php endforeach; ?>
</ul>
</div>
</div>
<button id="back-home" onclick="location.href='welcome.php'">Back to Home Page</button>
<script src="calendar.js"></script>
</body>
</html>