eevan79 Posted July 14, 2010 Share Posted July 14, 2010 How to get same dates from mysql and with php? On localhost I have current date for both. But when upload script I have time difference - with php I need +6 hours and in mysql +2 hours. $time = strtotime("+6 hours"); date("d.m.Y, H:i",$time); and I got: 14.07.2010, 03:30 When I insert something in mysql table with NOW() I have: 14.07.2010, 01:30 How to synchronize both dates to work on all servers? Link to comment https://forums.phpfreaks.com/topic/207675-php-and-mysql-dates/ Share on other sites More sharing options...
joel24 Posted July 14, 2010 Share Posted July 14, 2010 have you set PHP and mysql timezones on your server? [php //timezone php putenv('TZ=Australia/Sydney'); //get offset, including daylight savings because mysql won't! $currentOffset = "+".(date("Z") / 60 / 60).":00"; //timezone mysql $update_tz = @mysql_query("SET time_zone = '$currentOffset'") or die(mysql_error()); [/code] Link to comment https://forums.phpfreaks.com/topic/207675-php-and-mysql-dates/#findComment-1085690 Share on other sites More sharing options...
eevan79 Posted July 14, 2010 Author Share Posted July 14, 2010 This require restart server or not? Anyway, I use following script to compare time difference in minutes: date("Y-m-d H:i:s",$time); $mytime = date("Y-m-d H:i:s",$time); $time2 = mysql_query("SELECT NOW()"); $datetime = mysql_fetch_assoc($time2); $servertime = $datetime['NOW()']; echo "<br >Time difference: ".(strtotime($mytime) - strtotime($servertime))/ 60 ." minutes"; //and set variable $s_timediff = (strtotime($mytime) - strtotime($servertime))/ 60; When INSERT into table I just use NOW() + INTERVAL $s_timediff MINUTE and its working fine without setting up server timezone. Link to comment https://forums.phpfreaks.com/topic/207675-php-and-mysql-dates/#findComment-1085802 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.