PHP Date() - Format the Date

The required format parameter in the date() function specifies how to format the date/time.
Here are some characters that can be used:

  • d - Represents the day of the month (01 to 31)
  • m - Represents a month (01 to 12)
  • Y - Represents a year (in four digits)
A list of all the characters that can be used in the format parameter, can be found in our PHP Date reference.
Other characters, like"/", ".", or "-" can also be inserted between the letters to add additional formatting:
<?php
echo date("Y/m/d") . "<br />";
echo date("Y.m.d") . "<br />";
echo date("Y-m-d")
?>

PHP Date() - Adding a Timestamp

The optional timestamp parameter in the date() function specifies a timestamp. If you do not specify a timestamp, the current date and time will be used.
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.

Syntax for mktime()

mktime(hour,minute,second,month,day,year,is_dst)
<?php
$tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y"));
echo "Tomorrow is ".date("Y/m/d", $tomorrow);
?>

PHP Date / Time Functions

PHP: indicates the earliest version of PHP that supports the function.

Function

Description

PHP

checkdate()

Validates a Gregorian date

3

date_default_timezone_get()

Returns the default time zone

5

date_default_timezone_set()

Sets the default time zone

5

date_sunrise()

Returns the time of sunrise for a given day / location

5

date_sunset()

Returns the time of sunset for a given day / location

5

date()

Formats a local time/date

3

getdate()

Returns an array that contains date and time information for a Unix timestamp

3

gettimeofday()

Returns an array that contains current time information

3

gmdate()

Formats a GMT/UTC date/time

3

gmmktime()

Returns the Unix timestamp for a GMT date

3

gmstrftime()

Formats a GMT/UTC time/date according to locale settings

3

idate()

Formats a local time/date as integer

5

localtime()

Returns an array that contains the time components of a Unix timestamp

4

microtime()

Returns the microseconds for the current time

3

mktime()

Returns the Unix timestamp for a date

3

strftime()

Formats a local time/date according to locale settings

3

strptime()

Parses a time/date generated with strftime()

5

strtotime()

Parses an English textual date or time into a Unix timestamp

3

time()

Returns the current time as a Unix timestamp

3