PHP Date

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 OutputPHP 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 2011jS F Y
25th Sep 2011jS M Y
05 Sep 2011d M Y
9, Sep 11j, M, y
Sep 25, 2011M d, Y
Mon 25th Sep 2011D jS M Y
Mon Sep 4 2011D M j Y
Monday, 25th Sep 2011l, jS M Y
Monday 25th September 2011l jS F Y
1st January 23:30jS F H:i
1st January 11:30 pmjS F H:i a
1st January 11:30 PMjS F H:m A
12/1/2011 08:39 PMn/j/Y H:i A
12/1/2011 8:10 amn/j/Y g:i a
12/1/2011 7:10n/j/Y G:i
12/1/2011 19:50:31n/j/Y H:i:s
12/1/2011 19:50:31 UTCn/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 ESTd/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(1355103182011);

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

?>

Comments

There are 0 responses. Why not add a comment? No registration required.

Leave a Comment

Enter Code

Refresh code

Menu

Firefox Addons