Jump to content

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.