PHP 迄今为止的 UTC 字符串
PHP UTC String to date
我有问题。在我的代码中,我有以下行:
$RunDateTimeGMT0 = "2017-12-31 23:00:00";
目标是获取前一个小时和下一个小时,所以我尝试了这个:
$RunDateTimeGMT0 = "2017-12-31 23:00:00";
$epochRunDateTimeGMT0 = strtotime($RunDateTimeGMT0);
$previousEpochDateTimeGMT0 = $epochRunDateTimeGMT0 - 3600;
$nextEpochDateTimeGMT0 = $epochRunDateTimeGMT0 + 3600;
但随后我得到以下结果:
previousEpochDateTimeGMT0 -> 2017-12-31 21:00:00
nextEpochDateTimeGMT0 -> 2017-12-31 23:00:00
由于我的时区 (+1)
,RunDateTimeGMT0
被转换为我时区的日期。
我想要以下结果:
validEpochDateTimeGMT0 -> 2017-12-31 22:00:00
nextEpochDateTimeGMT0 -> 2018-01-01 00:00:00
如何保持日期对象 UTC?
您可以使用 Carbon
库。导入这个库后你应该用这个解析日期:
$RunDateTimeGMT0 = "2017-12-31 23:00:00";
$epochRunDateTimeGMT0 = Carbon::parse($RunDateTimeGMT0);
在this link中,你可以看到Carbon的文档。虽然,也许你应该使用这种方法:
$previousEpochDateTimeGMT0 = $epochRunDateTimeGMT0->addminutes(60);
$nextEpochDateTimeGMT0 = $epochRunDateTimeGMT0->subminutes(60);
我希望你的问题用这些行解决,如果出现其他问题你可以问。
@Ebrahim Bashirpour 已经分享了使用 Carbon 库执行此操作的方法,但您也可以仅使用 PHP 日期时间 class 来执行此操作。它支持两者 add time and subtracts time in seconds. Take a look at the DateTime documentation 以获取更多详细信息。
<?php
$RunDateTimeGMT0 = "2017-12-31 23:00:00";
$date = new \DateTime($RunDateTimeGMT0);
$date->add(new \DateInterval('PT3600S')); //add 3600s / 1 hour
$next_epoc = $date->format('Y-m-d H:i:s'); // 2018-01-01 00:00:00
$date = new \DateTime($RunDateTimeGMT0);
$date->sub(new \DateInterval('PT3600S'));//add 3600s / 1 hour
$previous_epoc = $date->format('Y-m-d H:i:s'); //2017-12-31 22:00:00
var_dump($next_epoc);
var_dump($previous_epoc);
?>
我有问题。在我的代码中,我有以下行:
$RunDateTimeGMT0 = "2017-12-31 23:00:00";
目标是获取前一个小时和下一个小时,所以我尝试了这个:
$RunDateTimeGMT0 = "2017-12-31 23:00:00";
$epochRunDateTimeGMT0 = strtotime($RunDateTimeGMT0);
$previousEpochDateTimeGMT0 = $epochRunDateTimeGMT0 - 3600;
$nextEpochDateTimeGMT0 = $epochRunDateTimeGMT0 + 3600;
但随后我得到以下结果:
previousEpochDateTimeGMT0 -> 2017-12-31 21:00:00
nextEpochDateTimeGMT0 -> 2017-12-31 23:00:00
由于我的时区 (+1)
,RunDateTimeGMT0
被转换为我时区的日期。
我想要以下结果:
validEpochDateTimeGMT0 -> 2017-12-31 22:00:00
nextEpochDateTimeGMT0 -> 2018-01-01 00:00:00
如何保持日期对象 UTC?
您可以使用 Carbon
库。导入这个库后你应该用这个解析日期:
$RunDateTimeGMT0 = "2017-12-31 23:00:00";
$epochRunDateTimeGMT0 = Carbon::parse($RunDateTimeGMT0);
在this link中,你可以看到Carbon的文档。虽然,也许你应该使用这种方法:
$previousEpochDateTimeGMT0 = $epochRunDateTimeGMT0->addminutes(60);
$nextEpochDateTimeGMT0 = $epochRunDateTimeGMT0->subminutes(60);
我希望你的问题用这些行解决,如果出现其他问题你可以问。
@Ebrahim Bashirpour 已经分享了使用 Carbon 库执行此操作的方法,但您也可以仅使用 PHP 日期时间 class 来执行此操作。它支持两者 add time and subtracts time in seconds. Take a look at the DateTime documentation 以获取更多详细信息。
<?php
$RunDateTimeGMT0 = "2017-12-31 23:00:00";
$date = new \DateTime($RunDateTimeGMT0);
$date->add(new \DateInterval('PT3600S')); //add 3600s / 1 hour
$next_epoc = $date->format('Y-m-d H:i:s'); // 2018-01-01 00:00:00
$date = new \DateTime($RunDateTimeGMT0);
$date->sub(new \DateInterval('PT3600S'));//add 3600s / 1 hour
$previous_epoc = $date->format('Y-m-d H:i:s'); //2017-12-31 22:00:00
var_dump($next_epoc);
var_dump($previous_epoc);
?>