PHP Date and Time

Last Updated : 24 Oct, 2026

PHP provides functions to format, manipulate, and compare dates and times, manage timestamps, and handle time zones.

  • Date Formatting: Display dates and times in different formats using date().
  • Timestamp Handling: Get the current Unix timestamp using time() and create timestamps using mktime().
  • Date Parsing: Convert human-readable date and time strings into timestamps using strtotime().
  • Time Zone Management: Retrieve and configure the default time zone using date_default_timezone_get() and date_default_timezone_set().

Getting the Current Date and Time

The date() function formats a date or time using the specified format string. If no timestamp is provided, it uses the current time.

Syntax:

date(format, timestamp);
  • format: Specifies how the date and time should appear.
  • timestamp: Optional Unix timestamp to format. If omitted, the current time is used.

Example : Display the Current Date

PHP
<?php
echo date("Y-m-d");
?>
  • Y: Four-digit year.
  • m: Month as a two-digit number.
  • d: Day of the month as a two-digit number.

Example 2: Display the Current Date and Time

PHP
<?php
   echo date("Y-m-d H:i:s"); 
?>
  • H: Hour in 24-hour format.
  • i: Minutes.
  • s: Seconds.

The displayed values depend on the current time and PHP's configured time zone.

Date Formatting Characters

The date() function accepts format characters that control how dates and times are displayed.

Character

Description

Example

d

Day of the month with leading zeros

04

D

Abbreviated day name

Fri

m

Month number with leading zeros

04

M

Abbreviated month name

Apr

y

Two-digit year

25

Y

Four-digit year

2025

h

Hour in 12-hour format

06

H

Hour in 24-hour format

18

i

Minutes

21

s

Seconds

50

a

Lowercase AM/PM indicator

pm

A

Uppercase AM/PM indicator

PM

Other characters, such as /, -, and ., can be added to separate date components.

Example: Display a Date in Different Formats

PHP
<?php
echo date("d/m/Y") . "\n";
echo date("d-m-Y") . "\n";
echo date("d.m.Y") . "\n";
echo date("d.M.Y/D");
?>

PHP time() Function

The time() function returns the current Unix timestamp, representing the number of seconds since January 1, 1970, at 00:00:00 UTC.

Syntax:

time();

Example: Get the Current Timestamp

PHP
<?php
$timestamp = time();

echo $timestamp . "\n";
echo date("F d, Y h:i:s A", $timestamp);
?>
  • time() returns the current Unix timestamp.
  • date() formats that timestamp into a readable date and time.
  • The formatted output uses PHP's default time zone.

Getting and Setting the Time Zone

PHP provides functions to retrieve and change the default time zone used by date and time functions.

  • date_default_timezone_get(): Returns the current default time zone.
  • date_default_timezone_set(): Sets the default time zone for the script.

Syntax:

date_default_timezone_get();
date_default_timezone_set(timezone);

Example: Set the Time Zone

PHP
<?php
date_default_timezone_set("America/New_York");

echo date_default_timezone_get() . "\n";
echo date("h:i:s A");
?>

The displayed time depends on the actual current time and the selected time zone.

Creating a Date with mktime()

The mktime() function returns a Unix timestamp for a specified date and time. PHP normalizes out-of-range date and time values automatically.

Syntax:

mktime(hour, minute, second, month, day, year);

Example: Create a Specific Date and Time

PHP
<?php
$timestamp = mktime(18, 21, 50, 4, 5, 2025);

echo date("Y-m-d h:i:s A", $timestamp);
?>

Creating a Date from a String with strtotime()

The strtotime() function converts a textual date and time description into a Unix timestamp.

Syntax:

strtotime(time, now);
  • time: A string describing a date or time.
  • now: Optional timestamp used as the reference for relative date expressions.

Example: Convert a Date String into a Timestamp

PHP
<?php
$timestamp = strtotime("3:52pm October 24 2026");

if ($timestamp !== false) {
    echo date("Y-m-d h:i:s A", $timestamp);
}
?>

The function returns false if the date string cannot be parsed. Always check the return value when processing user-provided date strings.

Comment