user007
user007

Reputation: 3243

Any PHP or MySQL function to get the Unix time without affecting the Timezone

I have stored a time in my database, for example this data: 2014-03-25 13:15:00

But when I use UNIX_TIMESTAMP(date_field) through mysql query or strtotime() in PHP, both functions output is effected by timezone.

Please tell me if it's possible to get unix time without timezone effect? It will also be very helpful if anyone can provide a PHP function.

Please do not suggest to me to change the timezone, because this is not a suitable solution.

Current output of 2014-03-25 14:05:00 when covert to unix and then again convert to date 2014-03-25 08:35:00

UPDATE

this not only happen only to DB time. like if I directly call this : date('Y-m-d H:i:s', strtotime(2014-03-25 14:05:00))

then it output

2014-03-25 08:35:00

Upvotes: 2

Views: 100

Answers (1)

Ja͢ck
Ja͢ck

Reputation: 173602

With PHP you can use DateTime:

$date = new DateTime('2014-03-25 13:15:00', new DateTimeZone('UTC'));

echo $date->format('Y-m-d H:i:s'); // 2014-03-25 13:15:00
echo $date->format('U'); // 1395753300

This assumes the times in your database are expressed in UTC.

Before PHP 5.3

$date = '2014-03-25 13:15:00';
echo gmdate('Y-m-d H:i:s', strtotime("$date GMT"));

Upvotes: 1

Related Questions