Jump to content

What is difference between php date and mysql NOW() ??


eevan79

Recommended Posts

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???

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! :]

 

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.