-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdate_calculator_php.php
More file actions
184 lines (171 loc) · 6.85 KB
/
Copy pathdate_calculator_php.php
File metadata and controls
184 lines (171 loc) · 6.85 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
<?php
$result = "";
$error = "";
if (isset($_POST["submit"])) {
$start = $_POST["start"];
$stop = $_POST["stop"];
if ($start == "" || $stop == "") {
$error = "Enter Stop and Start date";
}
if ($start != "" && $stop != "") {
//GETTING THE VALUE
$start = $_POST["start"];
$stop = $_POST["stop"];
$exstart = explode("-", $start);
$exstop = explode("-", $stop);
$startDay = $exstart[2];
$startMonth = $exstart[1];
$startYear = $exstart[0];
$stopDay = $exstop[2];
$stopMonth = $exstop[1];
$stopYear = $exstop[0];
//CONVERTING START MONTH TO DAY
$daysFromStartMonth = 0;
for ($i = (1 + $startMonth); $i <= 12; $i++) {
if ($i == 1 || $i == 3 || $i == 5 || $i == 7 || $i == 8 || $i == 10 || $i == 12) {
$daysFromStartMonth = 31 + $daysFromStartMonth;
}
if ($i == 4 || $i == 6 || $i == 9 || $i == 11) {
$daysFromStartMonth = 30 + $daysFromStartMonth;
}
if ($i == 2) {
if ($startYear % 4 == 0) {
$daysFromStartMonth = 29 + $daysFromStartMonth;
}
if ($startYear % 4 != 0) {
$daysFromStartMonth = 28 + $daysFromStartMonth;
}
}
}
//CONVERTING STOP MONTH TO YEAR
$daysBeforeStopMonth = 0;
for ($i = 1; $i <= ($stopMonth - 1); $i++) {
if ($i == 1 || $i == 3 || $i == 5 || $i == 7 || $i == 8 || $i == 10 || $i == 12) {
$daysBeforeStopMonth = 31 + $daysBeforeStopMonth;
}
if ($i == 4 || $i == 6 || $i == 9 || $i == 11) {
$daysBeforeStopMonth = 30 + $daysBeforeStopMonth;
}
if ($i == 2) {
if ($stopYear % 4 == 0) {
$daysBeforeStopMonth = 29 + $daysBeforeStopMonth;
}
if ($stopYear % 4 != 0) {
$daysBeforeStopMonth = 28 + $daysBeforeStopMonth;
}
}
}
//CONVERTING YEAR TO DAY
$daysInYear = 0;
for ($i = (1 + $startYear); $i <= ($stopYear - 1); $i++) {
if ($i % 4 == 0) {
$daysInYear = 366 + $daysInYear;
} elseif ($i % 4 != 0) {
$daysInYear = 365 + $daysInYear;
}
}
//SAME YEAR
$daysDiffInMonthSameYear = 0;
if ($startYear == $stopYear) {
for ($i = (1 + $startMonth); $i < $stopMonth; $i++) {
if ($i == 1 || $i == 3 || $i == 5 || $i == 7 || $i == 8 || $i == 10 || $i == 12) {
$daysDiffInMonthSameYear = 31 + $daysDiffInMonthSameYear;
}
if ($i == 4 || $i == 6 || $i == 9 || $i == 11) {
$daysDiffInMonthSameYear = 30 + $daysDiffInMonthSameYear;
}
if ($i == 2) {
if ($startYear % 4 == 0) {
$daysDiffInMonthSameYear = 29 + $daysDiffInMonthSameYear;
}
if ($startYear % 4 != 0) {
$daysDiffInMonthSameYear = 28 + $daysDiffInMonthSameYear;
}
}
}
}
//DETERMINE THE TOTAL DAYS IN START MONTH
if ($startMonth == 1 || $startMonth == 3 || $startMonth == 5 || $startMonth == 7 || $startMonth == 8 || $startMonth == 10 || $startMonth == 12) {
$daysInStartMonth = 31;
}
if ($startMonth == 4 || $startMonth == 6 || $startMonth == 9 || $startMonth == 11) {
$daysInStartMonth = 30;
}
if ($startMonth == 2) {
if ($startYear % 4 == 0) {
$daysInStartMonth = 29;
}
if ($startYear % 4 != 0) {
$daysInStartMonth = 28;
}
}
//DETERMINE THE REMAINING DAYS IN START MONTH
$daysRemInStartMonth = $daysInStartMonth - $startDay;
//TOTAL DAYS IN FIRST YEAR
$daysInFirstYear = $daysRemInStartMonth + $daysFromStartMonth;
//TOTAL DAYS IN LAST YEAR
$daysInLastYear = $daysBeforeStopMonth + $stopDay;
//total days for same year
function days($days)
{
if ($days == 0) {
return $days = "Same dates";
}
if ($days == 1) {
return $days = $days . " day";
}
if ($days > 1) {
return $days = $days . " days";
}
}
if (intval($stopYear) == intval($startYear)) {
//total days for SAME MONTH SAME YEAR
if (intval($startMonth) == intval($stopMonth)) {
$totaldays = ($stopDay - $startDay);
}
//total days for DIFFERENT MONTH SAME YEAR
if (intval($startMonth) != intval($stopMonth)) {
$totaldays = ($daysRemInStartMonth + $daysDiffInMonthSameYear + $stopDay);
}
}
//total days for different years
if (intval($stopYear) != intval($startYear)) {
$totaldays = ($daysInFirstYear + $daysInYear + $daysInLastYear);
}
$result = days($totaldays);
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.cdnfonts.com/css/poppins" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css">
<title>PHP Date Calculator</title>
</head>
<style>body {font-family: poppins;}</style>
<body>
<div class="container">
<div class="layout">
<h1 class="mb-5">Date Calculator</h1>
<form action="date_calculator_php.php" method="post">
<div class="mb-3 d-flex gap-3 align-items-center">
<label for="start">Start Date:</label>
<input type="date" name="start" class="form-control w-auto">
</div>
<div class="mb-3 d-flex gap-3 align-items-center">
<label for="stop">Stop Date:</label>
<input type="date" name="stop" class="form-control w-auto">
</div>
<button type="submit" class="btn btn-primary mb-3">Calculate</button>
</form>
<label for="days">Differences (days)</label><br>
<input type="text" disabled id="days" class="form-control" value="<?php echo $result; ?>" style="width: 300px;">
<p><?php echo $error; ?></p>
</div>
</div>
</body>
</html>