When Time Began: The PHP Date Function
PHP has a wonderful function called date(). It simply returns a date, in the format you request, and you can either print it or store it in a variable for later. On its face the date() function is quite simple, but it can be incredibly flexible as well. Let’s take a look at some options.
The formatting of the date output is controlled simply by giving it some letters, like this:
<?php echo date( ‘j F, Y’ ); ?>
That would echo something like 2 January, 2015. There are a wide variety of options you can pass for formatting, take a look at the documentation at http://php.net/date/. You should see something like the image below:
A practical use of this can be putting something like this in your footer:
Copyright <?php echo date(‘Y’); ?>
And then your footer will always have a copyright with the current year in it, without you needing to make any changes.
We saw another practical use in a recent blog post.
We used this combination of characters:
j F, G A
That combination produced a date and time which looks like this:
28 December, 3 pm
Time Began on January 1, 1970
When the creators of Unix released it, they wanted to give it an internal clock. It’s really a timer that counts up from 0. They needed to pick a time for that to start, so they chose the very first second of January 1, 1970.
Pro tip: this is why when you have a poorly formatted date you get 31 December, 1969.
This beginning of time is called the Unix Epoch. Many applications have access to this timestamp, including PHP. If you do this …
<?php echo date( ‘U’ ); ?>
… or this …
<?php echo time(); ?>
… you’ll get a print of how many seconds have transpired since the Unix Epoch. You can use this timestamp to do any number of things.
Turning Plain English into a Timestamp
PHP has a wonderful function called strtotime. You can feed it a date in plain English and it’ll return a Unix timestamp. Here are some examples:
<?php
echo strtotime(“now”), “n”;
echo strtotime(“10 September 2000”), “n”;
echo strtotime(“+1 day”), “n”;
echo strtotime(“+1 week”), “n”;
echo strtotime(“+1 week 2 days 4 hours 2 seconds”), “n”;
echo strtotime(“next Thursday”), “n”;
echo strtotime(“last Monday”), “n”;
?>
So, calculating next week’s date is as simple as typing “+1 week” into a strtotime function.
This gives you easy access to a Unix Timestamp any time you want it, which can be VERY powerful.
Putting It All Together
The date() function typically returns the current time, but it accepts a second parameter of a Unix Timestamp, like this:
<?php echo date( ‘j F, Y’ ), 1420313311 ) ); ?>
That’s not very comfortable though, since you need to acquire the timestamp you’re looking for. That’s where strotime() comes in. Let’s try something like this:
<?php echo date( ‘j F, Y’ ), strtotime( ‘last sunday’ ) ) ); ?>
Now, I wrote this post on January 2nd, so we’ll get a date like this:
28 December, 2014
This can be particularly useful in a number scenarios:
- You have an event that happens every Sunday, and you want to print the date of the most recent Sunday.
- You have a date in one format ( Jan 19, 99 ) and you want it in another format ( 19 January, 1999 ).
- You have a database with a date column that’s actually a plain text column, with dates in random formats, and you want a standard format.
Summary
The date() function is great for printing dates, and strtotime() is perfect for getting the date you wish to print.
Put them together and you have an incredibly effective solution for rendering dates in the right format.
Can you use these functions to create a simple countdown without the need for a component?