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.
Parameter | Description | Example |
D | Represents the day of the month in numeric format with leading zero (01 to 31) | echo date(“d”); |
D | Represents the day of the month in text format with three letters of day (Mon, Tue etc…). | echo date(“D”); |
J | Represents the day of the month in numeric format without leading zero (1 to 31). | echo date(“j”); |
l | Represents the day of the month in text format with all letters of day (Monday, Tuesday etc…). | echo date(“l”); |
m | Represents the month of the year in numeric format with leading zero (01 to 12). | echo date(“m”); |
M | Represents the month of the year in text format with three letters of month (Jan, Feb etc…). | echo date(“M”); |
F | Represents the month of the year in text format with all letters of month (January, February etc…). | echo date(“F”); |
n | Represents the month of the year in numeric format without leading zero (1 to 12). | echo date(“n”); |
y | Represents the year in two digits (01, 02, etc….). | echo date(“y”); |
Y | Represents the year in four digits (2001, 2002, etc….). | echo date(“Y”); |
A or a | Represents the current time is AM or PM, am or pm | echo date(“a”);echo date(“A”); |
g | Represents the hour of time in 12-hour format without leading zero (1 to 12). | echo date(“g”); |
G | Represents the hour of time in 24-hour format without leading zero (0 to 23). | echo date(“G”); |
h | Represents the hour of time in 12-hour format with leading zero (01 to 12). | echo date(“h”); |
H | Represents the hour of time in 24-hour format with leading zero (00 to 23). | echo date(“H”); |
i | Represents the minute of time with leading zero (00 to 59). | echo date(“i”); |
s | Represents 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