Dates are so much part of daily life that it evolves easily to work with them without assuming. PHP even supports effective tools for date arithmetic that make manipulating dates easy.
The PHP time() Function
PHP’s time() function is used to get all the details that we require about the current date and time. It needs no arguments but returns an integer.
The integer returned by time() means the number of seconds elapsed since midnight GMT on January 1, 1970. This point is the UNIX epoch
Example
<?php print time(); ?>
Output
The result will be here −
This is something complex to understand. But PHP offers superior functions to convert a timestamp for human readability.
We introduce date() function here.
The PHP Date() Function
The PHP date() function formats a timestamp to a human-readable date and time.
Syntax
date(format,timestamp)
The date() optionally gets a timestamp if omitted then the current date and time will be used. Any additional data you contain in the format string passed to date() will be formed in the return value.
Example
<h2>PHP Get Current Date</h2> <?php $today = date("d/m/Y"); echo "Today is " .$today; ?>
Output
The result will be here −
PHP Get Current Date
Today is 04/05/2022
Get a Date and Time
The necessary format parameter of the date() function defines how to format the date (or time).
Here are some the date and time interconnected formatting signs that are generally used in format string:
Format | Description | Example |
---|---|---|
a | ‘am’ or ‘pm’ lowercase | pm |
A | ‘AM’ or ‘PM’ uppercase | PM |
d | Day of the month, a number with leading zeroes | 20 |
D | Day of week (three letters) | Thu |
F | Month name | January |
h | Hour (12-hour format – leading zeroes) | 12 |
H | Hour (24-hour format – leading zeroes) | 22 |
g | Hour (12-hour format – no leading zeroes) | 12 |
G | Hour (24-hour format – no leading zeroes) | 22 |
i | Minutes ( 0 – 59 ) | 23 |
j | Day of the month (no leading zeroes | 20 |
l (Lower ‘L’) | Day of the week | Thursday |
L | Leap year (‘1’ for yes, ‘0’ for no) | 1 |
m | The month of the year (number – leading zeroes) | 1 |
M | The month of the year (three letters) | May |
r | The RFC 2822 formatted date | Wed, 04 May 2022 11:05:29 +0000 |
n | The month of the year (number – no leading zeroes) | 2 |
s | Seconds of hour | 20 |
U | Timestamp | 948372444 |
y | Year (two digits) | 05 |
Y | Year (four digits) | 2022 |
z | Day of year (0 – 365) | 123 |
Z | Offset in seconds from GMT | +5 |
Try out some of the following example:
<!DOCTYPE html> <html> <body> <h2>PHP Get Current Date and Time</h2> <?php echo date("h:i:s") . "<br>"; echo "Today is " .date("F d, Y h:i:s A"). "<br>"; echo date("h:i a"); ?> </body> </html>
Output
The result will be here −
PHP Get Current Date and Time
11:14:04
Today is: May 04, 2022 11:14:04 AM
11:14 am
The PHP mktime() Function
The mktime() function is applied to convert the timestamp based on a specific date and time. When the date and time are not given, the timestamp for the current date and time is returned.
Syntax for mktime()
mktime(hour,minute,second,month,day,year,is_dst)
Example
<!DOCTYPE html> <html> <body> <?php // Create the timestamp for a 3:20:12 pm on May 5, 2022 echo mktime(15, 20, 12, 5, 5, 2022); ?> </body> </html>
Output
The result will be here −
Others Examples of mktime() Function
Example
<!DOCTYPE html> <html> <body> <?php $tomorrow = mktime(0,0,0,date("m"),date("d")+1,date("Y")); echo "Tomorrow is ".date("Y/m/d", $tomorrow); ?> </body> </html>
Example
<!DOCTYPE html> <html> <body> <?php // Get the weekday name.. echo date('l', mktime(0, 0, 0, 5, 7, 2022)); ?> </body> </html>