PHP - Date & Time
PHP - Date & Time
Dates are so much part of everyday life that it becomes easy to work with them without thinking.
PHP also provides powerful tools for date arithmetic that make manipulating dates easy.
PHP's time() function gives you all the information that you need about the current date and
time. It requires no arguments but returns an integer.
The integer returned by time() represents the number of seconds elapsed since midnight GMT on
January 1, 1970. This moment is known as the UNIX epoch, and the number of seconds that
have elapsed since then is referred to as a time stamp.
Live Demo
<?php
print time();
?>
1480930103
This is something difficult to understand. But PHP offers excellent tools to convert a time stamp
into a form that humans are comfortable with.
The function getdate() optionally accepts a time stamp and returns an associative array
containing information about the date. If you omit the time stamp, it works with the current time
stamp as returned by time().
Following table lists the elements contained in the array returned by getdate().
seconds
1 20
Seconds past the minutes (0-59)
minutes
2 29
Minutes past the hour (0 - 59)
3 hours 22
Hours of the day (0 - 23)
mday
4 11
Day of the month (1 - 31)
wday
5 4
Day of the week (0 - 6)
mon
6 7
Month of the year (1 - 12)
year
7 1997
Year (4 digits)
yday
8 19
Day of year ( 0 - 365 )
weekday
9 Thursday
Day of the week
month
10 January
Month of the year
0
11 948370048
Timestamp
Now you have complete control over date and time. You can format this date and time in
whatever format you wan.
Example
Live Demo
<?php
$date_array = getdate();
seconds = 10
minutes = 29
hours = 9
mday = 5
wday = 1
mon = 12
year = 2016
yday = 339
weekday = Monday
month = December
0 = 1480930150
Today's date: 5/12/2016
The date() function returns a formatted string representing a date. You can exercise an enormous
amount of control over the format that date() returns with a string argument that you must pass to
it.
date(format,timestamp)
The date() optionally accepts a time stamp if omitted then current date and time will be used.
Any other data you include in the format string passed to date() will be included in the return
value.
Following table lists the codes that a format string can contain −
a
1 pm
'am' or 'pm' lowercase
A
2 PM
'AM' or 'PM' uppercase
d
3 20
Day of month, a number with leading zeroes
D
4 Thu
Day of week (three letters)
F
5 January
Month name
h
6 12
Hour (12-hour format - leading zeroes)
H
7 22
Hour (24-hour format - leading zeroes)
g
8 12
Hour (12-hour format - no leading zeroes)
G
9 22
Hour (24-hour format - no leading zeroes)
i
10 23
Minutes ( 0 - 59 )
j
11 20
Day of the month (no leading zeroes
l (Lower 'L')
12 Thursday
Day of the week
L
13 1
Leap year ('1' for yes, '0' for no)
m
14 1
Month of year (number - leading zeroes)
M
15 Jan
Month of year (three letters)
r Thu, 21 Dec 2000
16
16:01:07 +0200
The RFC 2822 formatted date
n
17 2
Month of year (number - no leading zeroes)
s
18 20
Seconds of hour
U
19 948372444
Time stamp
y
20 06
Year (two digits)
Y
21 2006
Year (four digits)
z
22 206
Day of year (0 - 365)
Z
23 +5
Offset in seconds from GMT
Example
Live Demo
<?php
print date("m/d/y G.i:s<br>", time());
echo "<br>";
print "Today is ";
print date("j of F Y, \a\\t g.i a", time());
?>
12/05/16 9:29:47
Today is 5 2016f December 2016 at 9:29 am
Hope you have good understanding on how to format date and time according to your
requirement. For your reference a complete list of all the date and time functions is given in PHP
Date & Time Functions.