When your website means business.

¡Important!, PHP | Type: PHPPHP Date/Time Guide

Useful php date/time formulas.

<?php echo date("Y-m-d"); ?> 2012-02-01
<?php echo date("Y/m/d"); ?> 2012/02/01
<?php echo date("m/d/y"); ?> 02/01/12 **
<?php echo date("M. d, Y"); ?> Feb. 01, 2012 (also, May. 01, 2013
<?php echo date("F d, Y"); ?> February 01, 2012
<?php echo date("D., M. j, Y"); ?> Wed., Feb. 1, 2012
<?php echo date("l, F j, Y"); ?> Wednesday, February 1, 2012 **
<?php echo date("l F d, Y, h:i:s"); ?> Wednesday February 01, 2012, 08:06:03
<?php echo date("l F d, Y, h:i A"); ?> Wednesday February 01, 2012, 08:06 PM

<?php date_default_timezone_set('Europe/Copenhagen');
echo 'The time in Copenhagen is: ' . date('H:i:s') . PHP_EOL; ?>
The time in Copenhagen is: 09:45:56

<?php date_default_timezone_set('America/New_York');
echo 'The time in New York is: ' . date('H:i:s') . PHP_EOL; ?>
The time in New York is: 03:45:56

<?php date_default_timezone_set('Europe/Moscow');
echo 'The time in Moscow is: ' . date('H:i:s') . PHP_EOL; ?>
The time in Moscow is: 11:45:56

What if you have users from multiple timezones? Still simple! Ask them what their timezone is and set it accordingly. The function timezone_identifiers_list() gives you an array of all the timezone identifiers, so you can use that to generate a drop down list they can choose from. You can also try out geolocating, but that is beyond the scope of this tutorial.

Finally, PHP has a function called gmdate(), which always formats the date in GMT. If you know the offset in seconds you can do like:
<?php gmdate('H:i:s', time() + $offset); ?>

so you might do
<?php gmdate('H:i:s', time() + 3600); ?> (1 hour)

Reference Links

We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.