Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the php timestamp is different from mysql UNIX_TIMESTAMP for a future date?

This is really strange for me.

I tried: <?php echo strtotime(date("Y-m-d H:i:s")); ?>

It returned: 1351498120.

Also, when i ran this query: SELECT UNIX_TIMESTAMP(now()) ,

it returned the same result: 1351498120.

But when i tried: <?php echo strtotime(date("2012-10-29 18:00:00")); ?>

It returns: 1351533600.

Whereas, if i run this query: SELECT UNIX_TIMESTAMP('2012-10-29 18:00:00'),

it returns: 1351513800

Now my question is: why the timestamps of php and mysql are same for current date, but different for future dates? Is there a way to compare them for future dates?

(NOTE: I have UTC as default timezone in php)

like image 993
shasi kanth Avatar asked Jan 15 '23 04:01

shasi kanth


2 Answers

There is a 5.5 hour difference between the 2, which indicates it is a timezone issue, on either end. The MySql Server's Timezone could be configured differently.

SET time_zone = timezonename;

can be used to set timezone for the current session. Check MySQL Date and Time Functions

like image 184
Anirudh Ramanathan Avatar answered Jan 17 '23 18:01

Anirudh Ramanathan


Try to set the default time zone

http://php.net/manual/en/function.date-default-timezone-set.php

like image 34
Jian Avatar answered Jan 17 '23 20:01

Jian