Select Page

Blue-toned image of programming
[toc]In my last article I explained how to format a date in MySQL using a built-in function that can be used during a database query. In this article I’m going to leave databases and talk about how to do the same thing in PHP.

The timestamp

Before you can format a date in PHP, you’re going to need something to work with and that something is a timestamp. A timestamp is the way that UNIX-based computers measured time, representing as a really big integer number. That big number is actually the number of seconds that have elapsed since January 1st, 1970 (which has the really catch name, The Unix Epoch).

Right now, as I write this, it’s 1463860298. Or, as normal people might say, 20:51 on Saturday, 21st May 2016.

To find out how many seconds have elapsed since the Unix Epoch, just use the time() function:

// Find out how many seconds have passed since 1st January, 1970
$now = time();

Displaying the timestamp as a date

I’m pretty sure most people would agree that 1463860298 is not a very useful way to express the current date and time. It’s handy for performing calculations (you can subtract one time from another and you’ll be left with the number of seconds between them) but not very readable.

To convert the timestamp to something more useful you can use the date() function.

date(format,(optional)timestamp);

The first parameter, format, is a string using special characters to show how you want to represent the date. The second parameter, the timestamp is optional – if you leave that out, the current date/time will be used.

Here’s an example that displays the current date and time in database format:

/* 
Y = 4 digit year - e.g. 2016
m = 2 digit month - e.g. 05 (May)
d = 2 digit date  - e.g. 21 (as in the 21st day of the month)
H = 2 digit hour in 24-hour format - e.g. 19 (7pm)
i = 2 digit minute - e.g. (39 as in 39 minutes past the hour)
s = 2 digit seconds
*/ 
$current_datetime = date('Y-m-d H:i:s');

Set your timezone

If you expect to see useful results, you’re going to have to set your local timezone. PHP feels so strongly about this that if you try to use the date functions without telling it where in the world you are, it will moan at you with this error message:

Warning: date() [function.date]: It is not safe to rely on the system’s timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function.

Fortunately, it’s really easy to resolve this. Just make sure that at the start of your PHP script, you use the date_default_timezone_set() function. Your server might already have it set in the PHP configuration (date.timezone in the php.ini file) so you might never see this error.

Here’s how I’d set the timezone for London (UK):

date_default_timezone_set('Europe/London');

The full list of acceptable timezones is available on the PHP manual page.

Example formats for some common date types

Below are some example formats you can use to represent common date types. I’ve not used the second parameter in the examples – obviously, if you have a timestamp you want to convert, pass that as the 2nd parameter. The formatting codes can be found on the PHP manual pages.

Date Style DATE_FORMAT syntax Output
MySQL
date('Y-m-d');
2016-05-31
US Short
date('m/d/Y');
05/31/2016
UK Short
date('d/m/Y');
31/05/2016
Short
date('d-M-y');
31-May-16
Long
date('l, dS F, Y');
Thursday, 31st May, 2016

In conclusion

It’s really easy to format dates the way you want them in PHP – just use the date function. Don’t forget – the formatting codes are available on the official PHP date manual page. I’ve only shown date formats – you can also represent times and other cool stuff too.