1

I have an old MySQL database. Here is a time column. I see here is some time values Like:

2013-06-03 21:33:15

So, I want to convert this time to my local time UTC +6 in my PHP Script. How can it possible? I can make a mysql query to get the time from Database to my my PHP variable $TimeFromMySQL Just now I want to show like:

11:32:44 PM 05 July 2014

Thank You

JEWEL AHMMED
  • 125
  • 1
  • 3
  • 11
  • 1
    Do you know the timezone of the original timestamp? Can't think of anyway you will be able to convert it to your local time without knowing that. – islanddave Jul 05 '14 at 17:42
  • Yes I know, When I run phpinfo(); in the server.. I see "Default timezone: America/Chicago" – JEWEL AHMMED Jul 05 '14 at 17:45
  • see http://stackoverflow.com/questions/15625834/how-to-convert-between-time-zones-in-php-using-the-datetime-class – islanddave Jul 05 '14 at 17:49
  • Why don't you want to put the "burden" on MySQL? – VMai Jul 05 '14 at 17:50
  • Not clear about the link's topic. Can you give me a answer with php code, because I am new in php. thank you. – JEWEL AHMMED Jul 05 '14 at 17:52
  • possible duplicate of [Convert timestamp to readable date/time PHP](http://stackoverflow.com/questions/5213528/convert-timestamp-to-readable-date-time-php) It runs on the timezone your server is set to. – a coder Jul 05 '14 at 17:59
  • MySQL does a lot with timestamp columns: **MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval.** If your time zone is the desired one you can simply use `DATE_FORMAT(your_timestamp_column, '%h:%i:%s %p %d %M %Y')` to get the output you want, see [manual, DATE_FORMAT](https://dev.mysql.com/doc/refman/5.6/en/date-and-time-functions.html#function_date-format). – VMai Jul 05 '14 at 18:05

2 Answers2

1

See VMai's comment above if you want to do this in MySQL. For PHP:

$inDate = '2013-06-03 21:33:15';
$inDate_tz = 'America/Chicago';

$original_date = new DateTime($inDate, new DateTimeZone($inDate_tz) );
$original_date->setTimeZone(new DateTimeZone('Asia/Dhaka'));
$new_date =  $original_date->format('H:i:s d F Y');

echo $new_date;

//outputs 08:33:15 04 June 2013
islanddave
  • 356
  • 2
  • 10
0

My answer here might be too late, still it might be helpful for some one who run to the same situation like me before I work around this solution.

To convert datetime to UTC that is, getting correct location Time and Date.

I came up with this:

// Get time Zone.
$whereTimeNow = date_default_timezone_get();
// Set the time zone.
date_default_timezone_set($whereTimeNow);

$dateTime = date('d-m-Y H:i');
$dateTimeConvert = new DateTime($dateTime, new DateTimeZone($whereTimeNow) );
$dateTimeConvert->setTimeZone(new DateTimeZone('UTC'));
$dateTimeNow =  $dateTimeConvert->format('Y-m-d H:i:s');
echo $dateTimeNow;
Bram
  • 2,515
  • 6
  • 36
  • 58
ShapCyber
  • 3,382
  • 2
  • 21
  • 27