PHP Date and Time

PHP has a rich set of functions for working with dates and times. These are essential for recording timestamps, scheduling events, calculating age or duration, formatting dates for display, and comparing time periods. PHP's date/time functions work with Unix timestamps — a number representing the count of seconds elapsed since January 1, 1970 (UTC).

Getting the Current Timestamp

The time() function returns the current Unix timestamp as an integer.

<?php
  $now = time();
  echo $now;   // Outputs something like: 1710000000
?>

The date() Function

The date() function formats a timestamp into a human-readable string. The first argument is a format string made up of format characters. The second argument is the timestamp; if omitted, it uses the current time.

<?php
  echo date("Y");             // Outputs: 2026  (4-digit year)
  echo date("m");             // Outputs: 03    (2-digit month)
  echo date("d");             // Outputs: 17    (2-digit day)
  echo date("H:i:s");         // Outputs: 14:30:00 (hour:minute:second, 24-hour)
  echo date("h:i A");         // Outputs: 02:30 PM  (12-hour format)
  echo date("l");             // Outputs: Tuesday  (full day name)
  echo date("D, d M Y");      // Outputs: Tue, 17 Mar 2026
  echo date("Y-m-d");         // Outputs: 2026-03-17 (ISO 8601 format)
  echo date("d/m/Y");         // Outputs: 17/03/2026
  echo date("F j, Y");        // Outputs: March 17, 2026
?>

Common Date Format Characters

CharacterMeaningExample
Y4-digit year2026
y2-digit year26
mMonth (01-12)03
nMonth (1-12, no leading zero)3
FFull month nameMarch
MAbbreviated month nameMar
dDay (01-31)17
jDay (1-31, no leading zero)17
lFull weekday nameTuesday
DAbbreviated weekday nameTue
HHour (00-23)14
hHour (01-12)02
iMinutes (00-59)30
sSeconds (00-59)00
AAM or PMPM
NDay of week (1=Mon, 7=Sun)2
tNumber of days in month31
UUnix timestamp1710000000

mktime() — Creating a Specific Timestamp

The mktime() function creates a Unix timestamp for a specific date and time. Arguments are: hour, minute, second, month, day, year.

<?php
  $christmas = mktime(0, 0, 0, 12, 25, 2026);
  echo date("l, F j, Y", $christmas);   // Outputs: Friday, December 25, 2026
?>

strtotime() — Convert a String to a Timestamp

The strtotime() function parses a human-readable date string and converts it to a Unix timestamp. It supports many flexible formats and relative expressions.

<?php
  $ts1 = strtotime("2026-06-15");
  echo date("D, d M Y", $ts1);   // Outputs: Mon, 15 Jun 2026

  $ts2 = strtotime("next Monday");
  echo date("d/m/Y", $ts2);

  $ts3 = strtotime("+30 days");
  echo date("Y-m-d", $ts3);      // 30 days from today

  $ts4 = strtotime("last Friday");
  echo date("Y-m-d", $ts4);

  $ts5 = strtotime("first day of next month");
  echo date("Y-m-d", $ts5);
?>

Calculating Date Differences

<?php
  $start = strtotime("2026-01-01");
  $end = strtotime("2026-12-31");

  $diffSeconds = $end - $start;
  $diffDays = round($diffSeconds / (60 * 60 * 24));

  echo "Days in 2026: " . $diffDays;   // Outputs: 364
?>

The DateTime Class

PHP provides an object-oriented approach to date handling through the DateTime class, which is more readable and powerful than procedural functions for complex date operations.

<?php
  // Create a DateTime object for a specific date
  $date = new DateTime("2026-03-17");
  echo $date->format("l, F j, Y");   // Tuesday, March 17, 2026

  // Current date/time
  $now = new DateTime();
  echo $now->format("Y-m-d H:i:s");

  // Add or subtract intervals
  $date->modify("+2 weeks");
  echo $date->format("Y-m-d");   // 2026-03-31

  $date->modify("-1 month");
  echo $date->format("Y-m-d");   // 2026-02-28
?>

Calculating Age with DateTime

<?php
  $birthdate = new DateTime("1995-08-20");
  $today = new DateTime();
  $age = $today->diff($birthdate);

  echo "Age: " . $age->y . " years";
?>

Setting the Timezone

PHP's date functions use the server's default timezone. Setting the timezone explicitly prevents inconsistencies.

<?php
  // Set for the current script
  date_default_timezone_set("America/New_York");
  echo date("H:i:s");   // Current time in New York

  // Or use DateTime with timezone
  $tz = new DateTimeZone("Asia/Tokyo");
  $tokyoTime = new DateTime("now", $tz);
  echo $tokyoTime->format("Y-m-d H:i:s");
?>

Key Points

  • time() returns the current Unix timestamp.
  • date("format", timestamp) formats a timestamp into a readable string.
  • mktime(h, m, s, month, day, year) creates a timestamp for a specific date.
  • strtotime("string") converts a human-readable date string to a timestamp — supports relative expressions like "next Monday" and "+7 days".
  • The DateTime class provides an object-oriented API with better support for complex date math.
  • Always set the timezone explicitly using date_default_timezone_set() or a DateTimeZone object.

Leave a Comment

Your email address will not be published. Required fields are marked *