这是我的问题
我的默认时区为date_default_timezone_set('Europe/Madrid')
actual time 2012-12-06 17:00:38
start time 2012-12-10 16:30:00
unix timestamp actual time 1354809638
unix timestamp start time 1350052200
开始时间大于实际时间。但是“unix timestamp实际时间”大于“unix timestamp start time”。
我想知道为什么?
相关代码:
$to_unix_startTime = new DateTime($postField['startTime']);
$UnixStartTime = $to_unix_startTime->format('U');
$ date = new DateTime(); $ actualTime = $ date-> getTimestamp();
$start = new DateTime($postFields['startAuct']);
$start->format('Y-m-d H:i:s').PHP_EOL;
$start_tmstmp = date_timestamp_get($start);
$date = new DateTime();
$timestamp = $date->getTimestamp();
if ($timestamp > $start_tmstmp) {
echo $auctStatus = 1;
} elseif ($timestamp < $start) {
echo $auctStatus = 4;
}
答案 0 :(得分:2)
$postField['startTime']
格式错误。我怎么知道这个?您从开始日期1350052200
获得的时间戳代表日期2012-10-12
(2012年10月12日),而不是2012-12-10
(2012年12月10日)。
$postField['startTime']
可能采用10/12/2012
格式(将DateTime
解析为美式日期),格式为10-12-2012
(将{被DateTime
解析为欧式日期。)
总而言之,检查$postField['startTime']
的格式并确保它是日期的欧式样式。
[edit] 比较:
$european = new DateTime( '10-12-2012' );
$american = new DateTime( '10/12/2012' );
echo $european->format( 'Y-m-d' ) . ' (' . $european->getTimestamp() . ')' . PHP_EOL;
echo $american->format( 'Y-m-d' ) . ' (' . $american->getTimestamp() . ')' . PHP_EOL;
答案 1 :(得分:0)
在获取时间戳之前无需格式化日期时间。
点击此处查看我的代码:https://compilr.com/shadyyx/datetime/index.php
如果链接有任何问题,这里是没有输出的代码:
<?php
date_default_timezone_set('Europe/Madrid');
$start = new DateTime('2012-12-10 16:30:00');
$actual = new DateTime('2012-12-06 17:00:38');
echo $start->format('d.m.Y H:i:s').PHP_EOL;
echo $actual->format('d.m.Y H:i:s').PHP_EOL;
echo PHP_EOL;
$start_tmstmp = $start->getTimestamp();
$act_tmstmp = $actual->getTimestamp();
echo $start_tmstmp.PHP_EOL;
echo $act_tmstmp.PHP_EOL;
echo PHP_EOL;
var_dump($start_tmstmp > $act_tmstmp);