Jump to content

php and mysql dates


eevan79

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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.