PHP Date and Time
PHP Date and Time Functions
PHP has many built-in date and time functions for handling dates and time.
The core functions for date and time is:
date()- formats a local date and/or a timemktime()- returns the Unix timestamp for a datestrtotime()- converts an English textual datetime string into a Unix timestamptime()- returns the current time as a Unix timestampdate_default_timezone_set()- sets the default timezone to be useddate_default_timezone_get()- gets the default timezone
The PHP Date() Function
The date() function is used to format a
local date and/or a time to a more readable date and time.
Syntax
date(format, timestamp)
| Parameter | Description |
|---|---|
| format | Required. Specifies the format of the timestamp |
| timestamp | Optional. Specifies a Unix timestamp. Default is the current local date and time |
Using the Date() Function
The required format parameter of the date() function specifies how to format the date (or time).
Here are some characters that are commonly used for dates:
- d - Represents the day of the month (01 to 31)
- j - Represents the day of the month (1 to 31)
- m - Represents a month (01 to 12)
- Y - Represents a year (in four digits)
- l (lowercase 'L') - Represents the day of the week
- F - Represents the month
Other characters, like"/", ".", or "-" can also be inserted between the format parameters to add additional formatting.
The example below formats the local date in five different ways:
Example
<?php
echo "Today is " . date("Y/m/d") . "<br>";
echo "Today is " . date("Y.m.d") . "<br>";
echo "Today is " . date("Y-m-d") . "<br>";
echo "Today is " . date("l"). "<br>";
echo date('l, F j, Y');
?>
Try it Yourself »
PHP Tip - Automatic Copyright Year
Use the date() function to automatically update the copyright year on your
website:
Display the Time
The date() function is
also used to format a local time to a more readable format.
Here are some characters that are commonly used for times:
- H - 24-hour format of an hour (00 to 23)
- h - 12-hour format of an hour with leading zeros (01 to 12)
- i - Minutes with leading zeros (00 to 59)
- s - Seconds with leading zeros (00 to 59)
- a - Lowercase am/pm
The example below outputs the current time in two different formats:
Example
<?php
echo "The time is " . date("H:i:s") . "<br>";
echo "The time is " . date("h:i:s a");
?>
Try it Yourself »
The PHP date_default_timezone_set() Function
Note that the date() function will return the current
local date/time of your server!
To get the date and time to be correct according to a specific
location, also set the timezone you want to use, with the
date_default_timezone_set()
function.
The example below sets the timezone to "America/New_York", then outputs the current date and time in the specified format:
Example
<?php
date_default_timezone_set("America/New_York");
echo "The current date and time is " . date("Y-m-d H:i:s");
?>
Try it Yourself »
The PHP date_default_timezone_get() Function
To return the default timezone used by all date/time functions in the script,
use the
date_default_timezone_get()
function:
The PHP mktime() Function
The optional timestamp parameter in the
date() function specifies a timestamp. If
omitted, the current date and time will be used (as in the
examples above).
The mktime() function returns the Unix timestamp for a date. The Unix timestamp
is the number of seconds between the Unix Epoch
(January 1 1970 00:00:00 GMT) and the time specified.
Syntax
mktime(hour, minute, second, month, day, year)
The following example returns the Unix timestamp for a date, and then use it to find the day of that specific date:
Example
<?php
// Set the default timezone to use
date_default_timezone_set("UTC");
$d = mktime(0, 0, 0, 10, 3, 1975);
echo "October 3, 1975 was on a " . date("l", $d);
?>
Try it Yourself »
The PHP time() Function
The time() function returns the
current time as a Unix timestamp. The Unix timestamp
is the number of seconds between the Unix Epoch
(January 1 1970 00:00:00 GMT) and the time specified.
Syntax
time()
Here we format the timestamp to a readable date and time:
Example
<?php
// Get the current Unix timestamp
$ts = time();
// Format timestamp
$curDate = date('Y-m-d H:i:s', $ts);
echo $curDate;
?>
Try it Yourself »
The PHP strtotime() Function
The strtotime() function converts
an English textual
datetime string into a
Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).
Syntax
strtotime(datetime_string, basetimestamp)
The
strtotime() function is quite clever about converting a string to a date, so you can put in various values:
Example
<?php
$d = strtotime("10:30pm November 15 2025");
echo "Date is " . date("Y-m-d H:i:s", $d) . "<br>";
$d = strtotime("now");
echo "Date is " . date("Y-m-d H:i:s", $d) . "<br>";
$d = strtotime("+5 days");
echo "Date is " . date("Y-m-d H:i:s", $d) . "<br>";
$d = strtotime("+2 weeks 4 days 2 hours 20 seconds");
echo "Date is " . date("Y-m-d H:i:s", $d) . "<br>";
$d = strtotime("last Sunday");
echo "Date is " . date("Y-m-d H:i:s", $d);
?>
Try it Yourself »
Tip: The strtotime()
is not perfect, so remember to check the strings you put in there!
More Date Examples
The following example outputs the dates for the next six Saturdays:
Example
<?php
$startdate = strtotime("Saturday");
$enddate = strtotime("+6 weeks", $startdate);
while ($startdate < $enddate) {
echo date("M d", $startdate) . "<br>";
$startdate = strtotime("+1 week", $startdate);
?>
Try it Yourself »
Complete PHP Date Reference
For a complete reference of all date functions, go to our complete PHP Date Reference.
The reference contains a brief description, and examples of use, for each function!