我正在一个项目中,用户可以在其中发布/申请职位。如果用户发布工作的交货时间为2天。在插入数据库时,我将在当前时间增加2天。然后我向用户显示一个倒数,该倒数显示了PHP和Javascript剩下多少天,几小时,几分,几秒。但是Javascript比PHP落后了5个小时。我已经问过这个问题,用户告诉我:这看起来像是时区差异问题。而且我必须在UTC中存储和处理日期。
For PHP to use UTC I did: date_default_timezone_set('UTC');
For Mysql: UTC_TIMESTAMP() + INTERVAL " . $job['delivery_time'] . "
For Javascript: var endDate = new Date("<?php echo $job['job_expiration']; ?>");
但是计时器比PHP,Mysql落后5个小时。
"INSERT INTO jobs(job_expiration) VALUES(UTC_TIMESTAMP() + INTERVAL " . $job['delivery_time'] . ")";
like so.
I'm using UTC_TIMESTAMP
and NOW()
so I can select the jobs where time is less than four hours without selecting all jobs.
I am using countdownjs for countdown.
我该如何解决?
<?php
$timestamp = strtotime($time['expiration']) - time();
function convert_timestamp_to_date($timestamp)
{
$date1 = new DateTime("@0");
$date2 = new DateTime("@$timestamp");
if ($date1->diff($date2)->d < 1) {
return $date1->diff($date2)->format('%h Hours');
} else {
return $date1->diff($date2)->format('%a Days');
}
}