PHP date format strings available for use with the PHP date function, copy and paste for formatting a date with PHP and the date function.
The PHP date function enables us to format the current date or a given date. PHP's date() function accepts an argument or parameter for the choosen date format, and a second optional argument for a given timestamp if you wish to format a specific date other than the current date. The date() function returns a formatted string. It is most commonly used for displaying the current or an event date on the front end.
See the example PHP date format strings below.
| Example Output | PHP Date format string |
|---|---|
| 12/1/2011 (US) | m/j/Y |
| 12/1/11 (US) | m/j/y |
| 02/01/2011 (US) | m/d/Y |
| 25/09/2011 (UK) | d/m/Y |
| 25th September 2011 | jS F Y |
| 25th Sep 2011 | jS M Y |
| 05 Sep 2011 | d M Y |
| 9, Sep 11 | j, M, y |
| Sep 25, 2011 | M d, Y |
| Mon 25th Sep 2011 | D jS M Y |
| Mon Sep 4 2011 | D M j Y |
| Monday, 25th Sep 2011 | l, jS M Y |
| Monday 25th September 2011 | l jS F Y |
| 1st January 23:30 | jS F H:i |
| 1st January 11:30 pm | jS F H:i a |
| 1st January 11:30 PM | jS F H:m A |
| 12/1/2011 08:39 PM | n/j/Y H:i A |
| 12/1/2011 8:10 am | n/j/Y g:i a |
| 12/1/2011 7:10 | n/j/Y G:i |
| 12/1/2011 19:50:31 | n/j/Y H:i:s |
| 12/1/2011 19:50:31 UTC | n/j/Y h:i:s e |
| 31/08/2011 19:50 +0200 (difference to GMT) | d/m/Y H:i O |
| 31/08/2011 19:50 EST | d/m/Y H:i T |
Use the above table to find your preferred PHP date() format. Just pass the format string as an argument to PHP's date() function.
PHP Code
<?php
echo date('jS F Y H:i a');
// outputs like: 2nd January 2011 11:30 pm
?>
Instead of using the current PHP date, the date function allows a timestamp to be passed to it.
PHP Code
<?php
// create a timestamp for 31st August 2011, 13:55:10 PM
$timestamp = mktime(13, 55, 10, 31, 8, 2011);
echo date('jS F Y H:i a', $timestamp);
// outputs like: 2nd January 2011 11:30 pm
?>
It's also possible to set the default timezone within your application using the date_default_timezone_set() function, like this:
PHP Code
<?php
date_default_timezone_set('Europe/London');
// or
date_default_timezone_set('America/New_York');
?>