In his tutorial, we are going to learn about the PHP Date and Time. The PHP
date() function is used to format a date and/or a time.
PHP 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.
The PHP Date() Function
date() function formats a timestamp to a more readable date and time.
|format||Required. Specifies the format of the timestamp|
|Specifies a timestamp. Default is the current date and time|
Get a Simple Date
The required format parameter of the date() function specifies how to format the date (or time).
Here are some characters that are commonly used for dates:
- d – Represents the day of the month (01 to 31)
- m – Represents a month (01 to 12)
- Y – Represents a year (in four digits)
- l (lowercase ‘L’) – Represents the day of the week
Other characters, like”/”, “.”, or “-” can also be inserted between the characters to add additional formatting.
The example below formats today’s date in three different ways:
<?php echo "Today is " . date("Y/m/d") . "<br>"; echo "Today is " . date("Y.m.d") . "<br>"; echo "Today is " . date("Y-m-d") . "<br>"; echo "Today is " . date("l"); ?>
Today is 2018/09/04 Today is 2018.09.04 Today is 2018-09-04 Today is Tuesday
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.
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.
Using our newly created timestamp, we can format it into a date that the user can understand.
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!
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)
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 the 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 the month, a number with leading zeroes||20|
|D||Day of the week (three letters)||Thu|
|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 the year (number – leading zeroes)||1|
|M||Month of the year (three letters)||Jan|
|r||The RFC 2822 formatted date||Thu, 21 Dec 2000 16:01:07 +0200|
|n||Month of the year (number – no leading zeroes)||2|
|s||Seconds of hour||20|
|y||Year (two digits)||06|
|Y||Year (four digits)||2006|
|z||Day of year (0 – 365)||206|
|Z||Offset in seconds from GMT||+5|
In this way, we can implement the PHP Date and Time in our PHP Program.