The PHP date()
function formats a timestamp into a more human-readable date and time.
date(format,timestamp) |
Parameter |
Description |
format |
Required. Specifies the format in which the timestamp will be displayed. |
timestamp |
Optional. Specifies a timestamp to format. Defaults to the current date and time if not provided. |
A timestamp is a sequence of characters that represents the date and/or time when a specific event took place. |
The required format
parameter of the date()
function determines how to format the date (or time).
Here are some commonly used characters for dates:
d
– Represents the day of the month (01 to 31)m
– Represents the month (01 to 12)Y
– Represents the year (in four digits)l
(lowercase ‘L’) – Represents the day of the weekOther characters, such as “/”, “.”, or “-“, can be added between these characters for additional formatting.
The example below shows today’s date formatted in three different ways:
<?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”); ?> |
Use the date()
function to automatically update the copyright year on your website:
© 2010-<?php echo date(“Y”);?> |
The optional timestamp
parameter in the date()
function specifies a particular timestamp. If it is not provided, the current date and time will be used (as shown in the examples above).
The PHP mktime()
function returns the Unix timestamp for a given date. This timestamp represents the number of seconds elapsed between the Unix Epoch (January 1, 1970, 00:00:00 GMT) and the specified time.
mktime(hour, minute, second, month, day, year) |
The example below uses the mktime()
function to generate a timestamp from several parameters and then creates a date and time with the date()
function:
mktime(hour, minute, second, month, day, year) |
The example below uses several parameters from the mktime()
function to generate a timestamp, which is then used by the date()
function to create a date and time.
<?php $d=mktime(11, 14, 54, 8, 12, 2014); echo “Created date is “ . date(“Y-m-d h:i:sa”, $d); ?> |
The PHP strtotime()
function converts a human-readable date string into a Unix timestamp, which represents the number of seconds since January 1, 1970, 00:00:00 GMT.
strtotime(time, now) |
The example below generates a date and time using the strtotime()
function.
<?php $d=strtotime(“10:30pm April 15 2014”); echo “Created date is “ . date(“Y-m-d h:i:sa”, $d); ?> |
PHP is quite flexible when converting a string to a date, allowing you to use various date formats and values.
<?php $d=strtotime(“tomorrow”); echo date(“Y-m-d h:i:sa”, $d) . “<br>”; $d=strtotime(“next Saturday”); echo date(“Y-m-d h:i:sa”, $d) . “<br>”; $d=strtotime(“+3 Months”); echo date(“Y-m-d h:i:sa”, $d) . “<br>”; ?> |
However, strtotime()
is not infallible, so be sure to verify the strings you pass to it.
The example below displays the dates for the next six Saturdays.
<?php $startdate = strtotime(“Saturday”); $enddate = strtotime(“+6 weeks”, $startdate); while ($startdate < $enddate) { echo date(“M d”, $startdate) . “<br>”; $startdate = strtotime(“+1 week”, $startdate); } ?> |
The example below calculates and displays the number of days remaining until the 4th of July.
<?php $d1=strtotime(“July 04”); $d2=ceil(($d1-time())/60/60/24); echo “There are “ . $d2 .” days until 4th of July.”; ?> |