PHP Date and Time

PHP Date and Time

The PHP date() function is used to format a date and/or a time.


formatRequired. Specifies the format of the timestamp
timestampOptional. Specifies a timestamp. Default is the current date and time


echo "Today's date is :"; 
$today = date("d-m-Y"); 
echo $today; 

Output: Today’s date is: 26-03-2020

Formatting options available in date() function:

The format parameter of the date() function is a string that can contain multiple characters allowing to generate dates in various formats.
Date-related formatting characters that are commonly used in the format string:

CharacterWhat It Means
dDay of the Month (Numeric 01 or 31)
DDay of the Week (String Mon to Sun)
lDay of the Week (String Mon to Sun)
FMonth (String Jan to Dec)
M Month (String Jan to Dec)
m Month (Numeric 01 to 12)
YYear in four-digit (Numeric 2008 or 2020 )
y Year in two-digit (Numeric 08 or 20 )
hHour (in 12-hour format)
H Hour (in 24-hour format)
aAM or PM

Example of Date Format

echo date("d/m/Y") . "n"; 
echo date("d-m-Y") . "n"; 
echo date("d.m.Y") . "n"; 
echo date("d.M.Y"); 



Example of Time Format

echo date("h:i:s") . "n"; 
echo date("M,d,Y h:i:s A") . "n"; 
echo date("h:i a");


Dec,26,2020  08:48:12 PM 
08:48 pm

PHP time() Function

The time() function is used to get the current time as a Unix timestamp. A UNIX timestamp for a particular time point represents the number of seconds that have elapsed between midnight on January 1970, and that time point. So, for example, the time point January 5 2008 10:15:00 AM in UNIX timestamp format would be 1199508300.

PHP can automatically turn a date value into a UNIX timestamp with mktime() function, which accepts day, month, year, hour, minute, and second arguments and returns a UNIX timestamp corresponding to that instant in time.

Below program explains usage of time() function in PHP:

$timestamp = time(); 
echo "n"; 
echo(date("F d, Y h:i:s A", $timestamp)); 



March 26, 2020 04:32:56 PM

Other Important Tutorials on PHP Date and Time

  • Date And Time Formatting With PHP
  • How to get yesterday and tomorrow date in PHP
  • How to change date format in PHP
  • How to get Current Indian time in PHP