Date()
FunctionThe PHP date()
function converts a timestamp into a more readable date and time format.
Computers store dates and times as UNIX Timestamps, which count time in seconds since midnight GMT on January 1, 1970 (Unix epoch). This format is impractical for human reading, so PHP converts timestamps into human-readable formats and vice versa. The syntax of the PHP date()
function is:
The format parameter in date()
specifies the format of the returned date and time. The timestamp parameter is optional; if not provided, the current date and time are used. The following statement displays today's date:
<?php
$today = date("d/m/Y");
echo $today;
?>
Note: The PHP date()
function retrieves the current date and time based on the internal clock of the web server where the script runs.
The format parameter of the date()
function is a string that includes various characters allowing you to generate a date string with different components such as day of the week, AM or PM, etc. Here are some common date-related formatting characters used in the format string:
You can separate parts of the date by adding characters like hyphens (-
), dots (.
), slashes (/
), or spaces for additional visual formatting.
<?php
echo date("d/m/Y") . "<br>";
echo date("d-m-Y") . "<br>";
echo date("d.m.Y");
?>
Tip: You can utilize the PHP date()
function to dynamically update the copyright year on your website, for example: Copyright © 2010-
.
Similarly, you can use the following characters to format the time string:
The PHP code in the following example demonstrates displaying the date in various formats:
<?php
echo date("h:i:s") . "<br>";
echo date("F d, Y h:i:s A") . "<br>";
echo date("h:i a");
?>
time()
FunctionThe time()
function retrieves the current time as a Unix timestamp (the number of seconds since the Unix epoch began: January 1, 1970, 00:00:00 GMT).
<?php
// Executed at March 05, 2014 07:19:18
$timestamp = time();
echo($timestamp);
?>
The above example produces the following output:
You can convert this timestamp to a human-readable date by passing it to the previously introduced date()
function.
<?php
$timestamp = 1394003958;
echo(date("F d, Y h:i:s", $timestamp));
?>
The above example produces the following output:
mktime()
FunctionThe mktime()
function creates a timestamp based on a specified date and time. If no date and time are provided, it returns the timestamp for the current date and time.
The syntax of the mktime()
function is:
For example, the following code displays the timestamp corresponding to 3:20:12 PM on May 10, 2014:
<?php
// Create the timestamp for a particular date
echo mktime(15, 20, 12, 5, 10, 2014);
?>
The above example produces the following output:
Note: You can omit as many arguments as needed in the mktime()
function, and it will use the current time for those omitted. If all arguments are omitted, mktime()
returns the UNIX timestamp for the current date and time, similar to time()
.
The mktime()
function can be utilized to determine the weekday name for a given date. Simply utilize the 'l' (lowercase 'L') character with your timestamp. For instance, consider the following example which showcases the day corresponding to April 1, 2014:
<?php
// Get the weekday name of a particular date
echo date('l', mktime(0, 0, 0, 4, 1, 2014));
?>
The above example produces the following output:
The mktime()
function can also be used to calculate a future date after a specified time period. For example, consider the following code which calculates the date that falls 30 months after the current date:
<?php
// Executed at March 05, 2014
$futureDate = mktime(0, 0, 0, date("m")+30, date("d"), date("Y"));
echo date("d/m/Y", $futureDate);
?>
The above example produces the following output:
For a comprehensive list of all the useful date and time functions available in PHP, please visit the PHP Date/Time Functions reference section.