【发布时间】:2014-05-13 19:19:32
【问题描述】:
我有一个应用程序需要发送 UTC 时间戳才能正常工作。在我的应用程序中,用户可以拥有任意数量的时区。因此,如果他们选择下午 3 点并且他们的时区是 America/New_York,则与选择 America/Chicago 时的 3 点不同。
我需要想办法将日期更改为正确的 UTC 时间戳。我知道我可以使用 date_default_timezone_set("UTC")...但我认为不会正常工作。
我想我需要计算 UTC 和常规时区之间的差异,但我不确定。欢迎任何建议。
date_default_timezone_set("UTC");
echo strtotime('5/13/2014 3:00 PM');
1399993200
date_default_timezone_set("America/New_York");
echo strtotime('5/13/2014 3:00 PM');
1400007600
你可以看出这两个值是不同的。
编辑:这是我的代码的样子。它似乎无法正常工作,因为应用程序没有在正确的时间显示事件。
$previous_timezone = date_default_timezone_get();
date_default_timezone_set("UTC");
$aceroute_schedule = $this->sale_lib->get_send_to_aceroute_schedule();
if (($start_time = strtotime($aceroute_schedule['aceroute_schedule_date']. ' '.$aceroute_schedule['aceroute_schedule_time_start'])) !== FALSE)
{
//Append 000 as as string for 32 bit systems
$start_epoch = $start_time.'000';
$end_epoch = strtotime('+ '.$aceroute_schedule['aceroute_duration'].' minutes', $start_time).'000';
}
else //Default to current time + 1 hour
{
//Append 000 as as string for 32 bit systems
$start_epoch = time().'000';
$end_epoch = strtotime('+1 hour', time()).'000';
}
$event->start_epoch = $start_epoch;
$event->end_epoch = $end_epoch;
【问题讨论】:
-
时间戳始终与时区无关——它们指的是某个时间点,而与当时您所在地区的挂钟时间无关。我不确定你的脑子里是否有条理的东西,也不太明白问题是什么。能不能表达的更清楚一点?