Local date format in PHP or MySQL

If locale is installed on the server then with PHP:

<?
//time zone from here http://php.net/manual/en/timezones.php
date_default_timezone_set('America/Panama');
//set locale
setlocale(LC_TIME, 'en_US.UTF8');
//reverse convert Unix timestamp to local date
echo strftime('%A, %d %B %Y %Z %x', time()); //Friday, 23 March 2012 EST 03/23/2012

//for function strtotime date should be GNU format http://www.gnu.org/software/shishi/manual/html_node/Date-input-formats.html
//reverse convert the local date to the Unix timestamp
echo strtotime('Friday, 23 March 2012'); //1332478800

For MySQL local date:

SET lc_time_names = 'en_US'; -- set locale
SET time_zone = '-5:00'; -- set time zone http://dev.mysql.com/doc/refman/5.5/en/time-zone-support.html
SELECT DATE_FORMAT(NOW(),'%e %M, %W'); -- 23 March, Friday
SELECT DATE_FORMAT(FROM_UNIXTIME(1242733660),'%e %M, %W'); -- 19 May, Tuesday
SELECT DATE_FORMAT(NOW(),'%M %D, %Y, %W, %l:%i %p'); -- March 23rd, 2012, Friday, 4:36 AM
This entry was posted in Programming and tagged , , , .

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.