PHP date functions: date, time, maketime

The date functions

The date function is an in-built function in PHP which takes zero or more arguments and based on that it provides the date in a human-readable format. The date function allows you to display format and manipulate date and time on the server using date function and PHP date format.

Syntax:-

<?php
	date( format [, timestamp])
?>

The table represents parameters of date function.

ParameterDescriptionExample
DRepresents the day of the month in numeric format with leading zero (01 to 31)echo date(“d”);
DRepresents the day of the month in text format with three letters of day (Mon, Tue etc…).echo date(“D”);
JRepresents the day of the month in numeric format without leading zero (1 to 31).echo date(“j”);
lRepresents the day of the month in text format with all letters of day (Monday, Tuesday etc…).echo date(“l”);
mRepresents the month of the year in numeric format with leading zero (01 to 12).echo date(“m”);
MRepresents the month of the year in text format with three letters of month (Jan, Feb etc…).echo date(“M”);
FRepresents the month of the year in text format with all letters of month (January, February etc…).echo date(“F”);
nRepresents the month of the year in numeric format without leading zero (1 to 12).echo date(“n”);
yRepresents the year in two digits (01, 02, etc….).echo date(“y”);
YRepresents the year in four digits (2001, 2002, etc….).echo date(“Y”);
A or aRepresents the current time is AM or PM, am or pmecho date(“a”);echo date(“A”);
gRepresents the hour of time in 12-hour format without leading zero (1 to 12).echo date(“g”);
GRepresents the hour of time in 24-hour format without leading zero (0 to 23).echo date(“G”);
hRepresents the hour of time in 12-hour format with leading zero (01 to 12).echo date(“h”);
HRepresents the hour of time in 24-hour format with leading zero (00 to 23).echo date(“H”);
iRepresents the minute of time with leading zero (00 to 59).echo date(“i”);
sRepresents the second of time with leading zero (00 to 59).echo date(“s”);

Example:

<?php
	echo date('Y-m-d H:i:s')
?>

Output

2020-06-07 20:09:56(Output can be different as this function showing up current date and time)

The time function

This function return current time stamp.

Syntax:-

<?php
	time();
?>

Example:

<?php
	echo time();
?>

Output:

1301545030

The maketime function

  • This function returns the current UNIX timestamp if no parameter is passed to this function.
  • If a particular date is passed as an argument it returns current UNIX timestamp for that date.

Syntax:-

<?php
	echo mktime(hour, minute, second, month, day, year);
?>

Example:-

<?php
	echo mktime(1,1,1,2,20,2012);
?>

Output:-

1329699661