Date And Time
Display Date
echo date("d-m-y"); // d = day of month, m = month of the year (#), y = year two digits
echo date("D-M-Y"); // D = day of week, M = month name, Y = year four digits
echo date("D M d, Y");
29-09-26
Tue-Sep-2026
Tue Sep 29, 2026
Tue-Sep-2026
Tue Sep 29, 2026
Display Time
echo date("h:i:s a");// h = hours with 12 hours format, i = minutes, s = seconds, a = lower case am pm
echo date("H:i:s A"); // H = hours with 24 hours format, i = minutes, s = seconds, A = upper case AM PM
02:51:21 pm
14:51:21 PM
14:51:21 PM
Display time in miliseconds
echo time();// display time in miliseconds
$echo = time("d-m-Y h:i:s A", $timeInMiliseconds); // convert miliseconds to date and time format
1790693481
29-09-2026 02:51:21 PM
29-09-2026 02:51:21 PM
Increment
$startDateTime = time(); $endDateTime = $startDateTime + 1000; // increment of 1000 miliseconds
echo . "Start Date Time: " . date("d-m-Y h:i:s A", $startDateTime); echo . "End Date Time: " . date("d-m-Y h:i:s A", $endDateTime);
Start Date Time: 29-09-2026 02:51:21 PM
End Date Time: 29-09-2026 03:08:01 PM
End Date Time: 29-09-2026 03:08:01 PM
Display Date and Time
echo date("d-m-Y h:i:s A");
29-09-2026 02:51:21 PM
Timestamp
$date = getdate(); foreach ($date as $format as $value) { echo $format . "=>" . $val; } echo $date()["month"]; // reading a specific part of the timestamp
echo getdate()["month"]; // reading a specific part of the timestamp directly from the getdate function.
seconds => 21
minutes => 51
hours => 14
mday => 29
wday => 2
mon => 9
year => 2026
yday => 271
weekday => Tuesday
month => September
0 => 1790693481
September
September
minutes => 51
hours => 14
mday => 29
wday => 2
mon => 9
year => 2026
yday => 271
weekday => Tuesday
month => September
0 => 1790693481
September
September
Date and Time with now, +1, and -1 Day
$yesterdaysDate = date_create("-1 day")->format("d-m-Y h:i:s a"); $todaysDate = date_create("now")->format("d-m-Y h:i:s a"); $tomorrowsDate = date_create("+1 day")->format("d-m-Y h:i:s a"); echo $yesterdaysDate; echo $todaysDate; echo $tomorrowsDate;
28-09-2026 02:51:21 pm
29-09-2026 02:51:21 pm
30-09-2026 02:51:21 pm
29-09-2026 02:51:21 pm
30-09-2026 02:51:21 pm
Convert String To DateTime
$stringDate = strtotime("2021-12-31 09:0:00"); // convert into miliseconds.
echo date("Y-m-d h:i:s a", $stringDate); // convert into date time format.
2021-12-31 09:00:00 am
DateTime Based on TimeZone
date_default_timezone_set ("America/Toronto"); $date = date_create ("now")->format ("America/Toronto"); echo $date;
29-09-2026 10:51:21 am
DateTime Difference
$startDate = date_create("2021-12-01"); $endDate = date_create("2021-12-31"); $timeDifference = date_diff($startDate, $endDate); echo $timeDifference;
30