eevan79 Posted July 31, 2010 Share Posted July 31, 2010 What is difference between php date(); and mysql NOW() and what should be used in script? In php date I have 13:00h, and from MySql have 17:00h. Current date on my comp is 19:00h. So time difference in php is +6 hours, and in mysql is +2 hours. How to synchronize this dates for users??? Quote Link to comment https://forums.phpfreaks.com/topic/209448-what-is-difference-between-php-date-and-mysql-now/ Share on other sites More sharing options...
Seaholme Posted July 31, 2010 Share Posted July 31, 2010 I'm no expert, but I guess whatever provides your MySQL is running in a different timezone to your site. You can fix the date() one to match the MySQL one with something like this: $timezone = new DateTimeZone( "Europe/London" ); $date = new DateTime(); $date->setTimezone( $timezone ); echo $date->format( 'H:i:s l, d. F Y' ); Basically it sets the timezone. So just name the timezone your MySQL server is on. I don't know if that's the right explanation or the best solution for your problem seeing as it's only really a cosmetic change, but it's what I would do! :] Quote Link to comment https://forums.phpfreaks.com/topic/209448-what-is-difference-between-php-date-and-mysql-now/#findComment-1093614 Share on other sites More sharing options...
xcoderx Posted July 31, 2010 Share Posted July 31, 2010 acording to my knowledge now() wil store both date and time bt date wil only date. Quote Link to comment https://forums.phpfreaks.com/topic/209448-what-is-difference-between-php-date-and-mysql-now/#findComment-1093617 Share on other sites More sharing options...
jcbones Posted July 31, 2010 Share Posted July 31, 2010 acording to my knowledge now() wil store both date and time bt date wil only date. Correct, NOW() is synonym for CURRENT_TIMESTAMP(), and CURDATE() is synonym for CURRENT_DATE(). Quote Link to comment https://forums.phpfreaks.com/topic/209448-what-is-difference-between-php-date-and-mysql-now/#findComment-1093619 Share on other sites More sharing options...
eevan79 Posted August 1, 2010 Author Share Posted August 1, 2010 I'm no expert, but I guess whatever provides your MySQL is running in a different timezone to your site. You can fix the date() one to match the MySQL one with something like this: $timezone = new DateTimeZone( "Europe/London" ); $date = new DateTime(); $date->setTimezone( $timezone ); echo $date->format( 'H:i:s l, d. F Y' ); Thanks, I'll try that. Still dont understand why is php and mysql dates different on same server. Thanks all for reply. Quote Link to comment https://forums.phpfreaks.com/topic/209448-what-is-difference-between-php-date-and-mysql-now/#findComment-1093780 Share on other sites More sharing options...
Mchl Posted August 1, 2010 Share Posted August 1, 2010 Because the person who was setting them up didn't care about matching their timezones. Quote Link to comment https://forums.phpfreaks.com/topic/209448-what-is-difference-between-php-date-and-mysql-now/#findComment-1093798 Share on other sites More sharing options...
eevan79 Posted August 1, 2010 Author Share Posted August 1, 2010 Now I must calculate time difference. I tried this code $timezone = new DateTimeZone( "Europe/London" ); $date = new DateTime(); $date->setTimezone( $timezone ); $res = mysql_query("UPDATE ".$table_prefix."users SET user_last_login = ".$date->format( $date_format )." WHERE user_id = $uid")or die(mysql_error()); Of course it doesnt work. I must use NOW() +/- INTERVAL. Can I use something like this to calculate time difference: $servertime = $datetime['NOW()']; $s_timediff = (strtotime($date->format( $date_format )) - strtotime($servertime))/ 60; Its working. Thanks all. Quote Link to comment https://forums.phpfreaks.com/topic/209448-what-is-difference-between-php-date-and-mysql-now/#findComment-1093806 Share on other sites More sharing options...
Mchl Posted August 1, 2010 Share Posted August 1, 2010 You know you can just change your MySQL server's timezone to match the one used in PHP (or vice versa)? http://dev.mysql.com/doc/refman/5.1/en/time-zone-support.html http://www.php.net/manual/en/function.date-default-timezone-set.php Quote Link to comment https://forums.phpfreaks.com/topic/209448-what-is-difference-between-php-date-and-mysql-now/#findComment-1093808 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.