Home » Tutorials » PHP » Date and Time

Date and Time

PHP Date and Times are just as frustrating as any other languages. The main issue of Date and Times is that they are entirely different strings that both have strong importance on position and number of characters. For example, a PHP date is usually in the format of “2013-02-01” and times always seem to vary. This is because, we as the great thinkers of the world, created a ridiculous time system with random values instead of some metric like construction. Of course, there is entirely a purpose for time’s current format, but we can both agree that it is not programmer friendly.

What we both know is that time and date is a rather useless distinction. In certain definitions of time, it also implies the date. So, from now on I want you to think of time also containing dates. It is much more useful to place these together and you probably will not give me a great reason to separate the two after this tutorial. First, let’s get into explaining timestamps and some PHP date and time functions.

PHP Timestamps

The PHP timestamp is a Unix timestamp that tracks the number of seconds since January 1, 1970 because we apparently believe milliseconds are useless. Nevertheless, let’s create a timestamp.

echo time();
Result 1359780799

So, we just use PHP’s time function with default parameters to create a timestamp of this very moment (well, the moment I created this tutorial). Why would we want something like that when users cannot understand its format? First, it is a very consistent and easy to manipulate number. Second, I will show you how to format the timestamp into PHP dates and times in just a second. Also, note if you just want the current time, you should use time, but you should use mktime for custom datetimes.

PHP Dates

Using our newly created timestamp, we can format it into a date that the user can understand.

echo date("Y-m-d","1359780799");
Result 2013-02-01

Boom! Now, we have a formatted date string that users can understand. Now I will go on a small tangent and emphasize my extreme approval of this particular format. Sure, “2013/02/01” is equally valid, but the hyphens seem to be more universal and easier to work with. I know I do not particularly provide a strong case for the date format, but if you have no preference, take mine!

PHP Times

Do you remember my tangent of times also include dates? Well, the guys that created PHP must have agreed with me. The PHP date function also can format timestamps into “Times”. Let’s see how to create a readable time.

echo date( "H:i:s","1359780799");
Result 21:53:19

Well done. Now, we have the current military time that I created this tutorial. I know you might be thinking, “How do I create a timestamp that is not the present?”. Relax, we will get to it in just a moment.

PHP Date and Time

Now, let’s mix it up a little bit and create our own time and not the current time. We want to format this into a full date and time string.

echo date("Y-m-d H:i:s",mktime(6,30,51,12,01,1999));
// mktime(hour,minute,second,month,day,year)
Result 1999-12-01 06:30:51

Now, that is a lot to digest. We have created a full date and time string using special formatting with the PHP date function. mktime actually has parameters this time, which is how we create a custom date and time that is not the present. In order, the mktime parameters are hour, minute, second, month, day, and year. See how much easier it is to work with timestamps instead of creating dates and times separately? The PHP date function has a lot of different formatting values. I have included the references below. Feel free to tinker around with this in the first parameter of the date function to see how the characters actually format the string.

PHP Date and Time Formats

a‘am’ or ‘pm’ (lowercase)pm
A‘AM’ or ‘PM’ (uppercase)PM
dDay of month, a number with leading zeroes20
DDay of week (three letters)Thu
FMonth nameJanuary
hHour (12-hour format – leading zeroes)12
HHour (24-hour format – leading zeroes)22
gHour (12-hour format – no leading zeroes)12
GHour (24-hour format – no leading zeroes)22
iMinutes ( 0 – 59 )23
jDay of the month (no leading zeroes20
l (Lower ‘L’)Day of the weekThursday
LLeap year (‘1’ for yes, ‘0’ for no)1
mMonth of year (number – leading zeroes)1
MMonth of year (three letters)Jan
rThe RFC 2822 formatted dateThu, 21 Dec 2000 16:01:07 +0200
nMonth of year (number – no leading zeroes)2
sSeconds of hour20
UTime stamp948372444
yYear (two digits)06
YYear (four digits)2006
zDay of year (0 – 365)206
ZOffset in seconds from GMT+5

Link/cite this page

If you use any of the content on this page in your own work, please use the code below to cite this page as the source of the content.

  • Stewart, Suzy. "Date and Time". After Hours Programming. Accessed on June 6, 2024. https://www.afterhoursprogramming.com/tutorial/php/date-and-time/.

  • Stewart, Suzy. "Date and Time". After Hours Programming, https://www.afterhoursprogramming.com/tutorial/php/date-and-time/. Accessed 6 June, 2024.

  • Stewart, Suzy. Date and Time. After Hours Programming. Retrieved from https://www.afterhoursprogramming.com/tutorial/php/date-and-time/.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.