简体   繁体   中英

Javascript Date Object Not Adjusting for Timezone Offset Properly

I'm working on an events site. I get UTC times from the MySQL database on my localhost and echo them to the page using PHP, and then translate them into the user's timezone using Javascript. For some reason, Javascript gives me a time that's two hours behind the time I got from MySQL. Both myself and my server are in the Mountain Standard Timezone, so Javascript should push the date back by six hours, but instead the date is going back by eight hours. Even Daylight Savings only accounts for a one hour difference, not two.

Here's the Javascript for translating the PHP timestamp to a date object and outputting it in YYYY-mm-dd HH:ii:ss format (uses jQuery):

function leadingzeros(string){
    if (string.toString().length == 1){
        string = "0" + string;
    }
    return string;
}

$(document).ready(function(){
    var thisdate = new Date(<?php echo (strtotime($row['start'])*1000);?>);
    $("#jsdate").html(
        thisdate.getFullYear()+"-"+leadingzeros((thisdate.getMonth()+1))+"-"+ leadingzeros(thisdate.getDate())+" "+
        leadingzeros(thisdate.getHours())+":"+leadingzeros(thisdate.getMinutes())+":"+leadingzeros(thisdate.getSeconds())
    );
});

Here's the PHP for the document body:

<?php
$row = mysqli_fetch_assoc($result);
echo "<ul><li>MySQL datetime object: ".$row['start']."</li>".
"<li>PHP Unix timestamp: ".strtotime($row['start'])."</li>".
"<li>PHP date string: ".date("Y-m-d H:i:s",strtotime($row['start']))."</li>".
"<li>PHP date minus 6 hours (which is our timezone offset): ".date("Y-m-d H:i:s",strtotime($row['start']) - (6*3600))."</li>".
"<li>Javascript date: <span id='jsdate'></span></li></ul>";
?>

It outputs:

  • MySQL datetime object: 2012-04-07 23:00:00
  • PHP Unix timestamp: 1333832400
  • PHP date string: 2012-04-07 23:00:00
  • PHP date minus 6 hours (which is our timezone offset): 2012-04-07 17:00:00
  • Javascript date: 2012-04-07 15:00:00

None of the documentation I've read yields answers. Why is the Javascript date two hours behind the PHP date in MST?

The reason why this happens is because strtotime() uses the date.timezone setting by default when it parses a given date / time, so the result gets compensated accordingly.

To resolve this, you can either set the date.timezone to Etc/UTC or "anchor" the time stamp from your database like so:

strtotime("{$row['start']} GMT")

I just alert the date in js using this code.

var thisdate = new Date(<?php echo (strtotime('2012-04-07 23:00:00')*1000);?>);
    alert(thisdate);
    alert(<?php echo (strtotime('2012-04-07 23:00:00')*1000);?>);

what i got was在此处输入图像描述

so u get time with respect to gmt.in php and mysql server must have configured time zone.so u don't have to worry.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM