The PHP date()
function formats a timestamp 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 timestamp. Default is the current date and time |
The required format
parameter of the date()
function specifies how to format the date (or time). This can be done using the following:
-
d
- the day of the month (01 to 31) -
m
- month (01 to 12) -
Y
- year (in four digits) -
l
(lowercase 'L') - the day of the week
Characters like "/", ".", or "-" can also be inserted between the characters to add additional formatting.
Below are examples of using the date()
function.
<?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>";
The time() Function
The time()
function is used to get the current time as a Unix timestamp (not human-friendly). It can then be converted to a human-readable date through passing it to the date()
function. The following is an example of this.
<?php
$timestamp = time();
echo(date("F d, Y h:i:s", $timestamp));
The mktime() Function
The mktime()
function returns the Unix timestamp for a date. The Unix timestamp contains the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified. If date and time are not provided, the timestamp for the current date and time is returned.
Syntax
mktime(hour, minute, second, month, day, year)
The example below creates a date and time with the date() function from a number of parameters in the mktime()
function:
<?php
$d=mktime(10, 15, 00, 05, 27, 2021);
echo "Created date is " . date("Y-m-d h:i:sa", $d);