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");
13-04-26
Mon-Apr-2026
Mon Apr 13, 2026
Mon-Apr-2026
Mon Apr 13, 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
01:57:27 pm
13:57:27 PM
13:57:27 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
1776088647
13-04-2026 01:57:27 PM
13-04-2026 01:57:27 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: 13-04-2026 01:57:27 PM
End Date Time: 13-04-2026 02:14:07 PM
End Date Time: 13-04-2026 02:14:07 PM
Display Date and Time
echo date("d-m-Y h:i:s A");
13-04-2026 01:57:27 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 => 27
minutes => 57
hours => 13
mday => 13
wday => 1
mon => 4
year => 2026
yday => 102
weekday => Monday
month => April
0 => 1776088647
April
April
minutes => 57
hours => 13
mday => 13
wday => 1
mon => 4
year => 2026
yday => 102
weekday => Monday
month => April
0 => 1776088647
April
April
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;
12-04-2026 01:57:27 pm
13-04-2026 01:57:27 pm
14-04-2026 01:57:27 pm
13-04-2026 01:57:27 pm
14-04-2026 01:57:27 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;
13-04-2026 09:57:27 am
DateTime Difference
$startDate = date_create("2021-12-01"); $endDate = date_create("2021-12-31"); $timeDifference = date_diff($startDate, $endDate); echo $timeDifference;
30