Date and time are so much part of everyday life to the point that it becomes very easy to work with them without thinking. PHP also provides powerful tools for date arithmetic that makes handling dates very easy.
Getting Time Stamp with time() Function
Example
<?php print time(); ?>
Output
1601898793
Converting a Time Stamp with getdate() Function
Following table list the elements contained in the array returned by getdate() function -
Sr.No | Key & Description | Example |
---|---|---|
1 | seconds Seconds past the minutes (0-59) | 20 |
2 | minutes Minutes past the hour (0 - 59) | 29 |
3 | hours Hours of the day (0 - 23) | 22 |
4 | mday Day of the month (1 - 31) | 11 |
5 | wday Day of the week (0 - 6) | 4 |
6 | mon Month of the year (1 - 12) | 7 |
7 | year Year (4 digits) | 1997 |
8 | yday Day of year ( 0 - 365 ) | 19 |
9 | weekday Day of the week | Thursday |
10 | month Month of the year | January |
11 | 0 Timestamp | 948370048 |
Example
<?php $date_array = getdate(); foreach ( $date_array as $key => $val ){ print "$key = $val<br />"; } $formated_date = "Today's date: "; $formated_date .= $date_array['mday'] . "/"; $formated_date .= $date_array['mon'] . "/"; $formated_date .= $date_array['year']; print $formated_date; ?>
Output
seconds = 50 minutes = 56 hours = 12 mday = 5 wday = 1 mon = 10 year = 2020 yday = 278 weekday = Monday month = October 0 = 1601902610 Today's date: 5/10/2020
Converting a Time Stamp with date() Function
date(format, timestamp)
Following table below list all the codes that a format string can hold -
Sr.No | Format & Description | Example |
---|---|---|
1 | a 'am' or 'pm' lowercase | pm |
2 | A 'AM' or 'PM' uppercase | PM |
3 | d Day of month, a number with leading zeroes | 20 |
4 | D Day of week (three letters) | Thu |
5 | F Month name | January |
6 | h Hour (12-hour format - leading zeroes) | 12 |
7 | H Hour (24-hour format - leading zeroes) | 22 |
8 | g Hour (12-hour format - no leading zeroes) | 12 |
9 | G Hour (24-hour format - no leading zeroes) | 22 |
10 | i Minutes ( 0 - 59 ) | 23 |
11 | j Day of the month (no leading zeroes | 20 |
12 | l (Lower 'L') Day of the week | Thursday |
13 | L Leap year ('1' for yes, '0' for no) | 1 |
14 | m Month of year (number - leading zeroes) | 1 |
15 | M Month of year (three letters) | Jan |
16 | r The RFC 2822 formatted date | Thu, 21 Dec 2000 16:01:07 +0200 |
17 | n Month of year (number - no leading zeroes) | 2 |
18 | s Seconds of hour | 20 |
19 | U Time stamp | 948372444 |
20 | y Year (two digits) | 06 |
21 | Y Year (four digits) | 2006 |
22 | z Day of year (0 - 365) | 206 |
23 | Z Offset in seconds from GMT | +5 |
Example
<?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()); ?>
Output
10/05/20 13.57:42 Today is 5 of October 2020, at 1.57 pm
Feel free to ask your questions where necessary and we will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.
Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.
Thanks for reading and bye for now.