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-08-26
Thu-Aug-2026
Thu Aug 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
12:52:27 pm
12:52: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
1786625547
13-08-2026 12:52: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-08-2026 12:52:27 PM
End Date Time:  13-08-2026 01:09:07 PM

Display Date and Time

echo date("d-m-Y h:i:s A");
13-08-2026 12:52: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 => 52
hours => 12
mday => 13
wday => 4
mon => 8
year => 2026
yday => 224
weekday => Thursday
month => August
0 => 1786625547

August
August

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-08-2026 12:52:27 pm
13-08-2026 12:52:27 pm
14-08-2026 12:52: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-08-2026 08:52:27 am

DateTime Difference

$startDate = date_create("2021-12-01");
$endDate = date_create("2021-12-31");
$timeDifference = date_diff($startDate, $endDate);

echo $timeDifference;
30