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();


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");


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");


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)

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
d Day of 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 Month of year (number – leading zeroes) 1
M Month of year (three letters) Jan
r The RFC 2822 formatted date Thu, 21 Dec 2000 16:01:07 +0200
n Month of year (number – no leading zeroes) 2
s Seconds of hour 20
U Time stamp 948372444
y Year (two digits) 06
Y Year (four digits) 2006
z Day of year (0 – 365) 206
Z Offset in seconds from GMT +5
If you enjoyed this resource, support me by sharing this page with others.
Share on Facebook
Share on StumbleUpon
Digg this
Tweet about this on Twitter
Share on Reddit

Leave a Reply

Your email address will not be published. Required fields are marked *

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