-1

The database stores values as YYYY-MM-DD HH:MM:SS (eg 2013-04-14%2015:00:00) which is in GMT time but the true value is the the above date and time + time zone difference of the server.

$time_heard = '2013-04-14%2015:00:00';
$time_diff = date('p')

$real_time = $time_heard + $time_diff  ;

echo $real_time ; 

How can i do this addition. Thanks

Charm_quark
  • 376
  • 2
  • 6
  • 30

2 Answers2

0

If you're using MySQL you can use the CONVERT_TZ functionality straight in the SELECT. http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_convert-tz

If you need to do it in PHP, use the DateTime class by creating a new DateTime with the timezone set to GMT then changing the timezone and when you output the date it should display as the new timezone.

George Yates
  • 1,237
  • 9
  • 16
  • i'm using PostgreSQL, and i do not know the time zone (as they can vary, on where the installation is done). the other issue is that the $time_heard variable could also be a user input. – Charm_quark Aug 12 '13 at 05:09
  • Then you're gonna have to use PHP. But the `$time_heard` can be used as user input when you're creating the DateTime object, just be sure to use an acceptable formatting. And to get the local server timezone, just use [date_default_timezone_get()](http://php.net/manual/en/function.date-default-timezone-get.php) – George Yates Aug 12 '13 at 05:28
0

you can use date_default_timezone_set() function to set the time to the zone you want

here you can find different zones: http://www.php.net/manual/en/timezones.php

or if you insist, you can use date_parse_from_format()

here is the manual for it: http://php.net/manual/en/function.date-parse-from-format.php

edit: another way to do it with easy functions:

$tmstamp = strtotime($date1) + strtotime($date2);
echo date("YYYY-MM-DD HH:MM:SS", $tmstamp) ;