Php Datetime在Datetime之前或之后
我有两个字符串,如下所示:Php Datetime在Datetime之前或之后,php,datetime,compare,Php,Datetime,Compare,我有两个字符串,如下所示: $stringA = '2017-05-05 12:06:00'; $stringB = '2017-04-05 16:32:00'; if($stringB > stringA) { echo "string B is still in the future!"; } 如何查看$stringB是在$stringA之前还是之后 我是这样想的: $stringA = '2017-05-05 12:06:00'; $stringB = '2017-04-
$stringA = '2017-05-05 12:06:00';
$stringB = '2017-04-05 16:32:00';
if($stringB > stringA) {
echo "string B is still in the future!";
}
如何查看$stringB
是在$stringA
之前还是之后
我是这样想的:
$stringA = '2017-05-05 12:06:00';
$stringB = '2017-04-05 16:32:00';
if($stringB > stringA) {
echo "string B is still in the future!";
}
这里我们使用
strotime
将时间转换为秒数,并比较两个日期的秒数
输出:
之前是2017-04-05 16:32:00
这里我们使用strotime
将时间转换为秒数,并比较两个日期的秒数
输出:
之前是2017-04-05 16:32:00
在PHP中,您可以使用日期时间
或日期时间不可变
对象
$stringA = new DateTimeImmutable('2017-05-05 12:06:00');
$stringB = new DateTimeImmutable('2017-04-05 16:32:00');
然后你就可以安全地完成你刚刚写的东西了
if ($stringB > stringA) {
echo "string B is still in the future!";
}
在PHP中,可以使用
DateTime
或DateTimeImmutable
对象
$stringA = new DateTimeImmutable('2017-05-05 12:06:00');
$stringB = new DateTimeImmutable('2017-04-05 16:32:00');
然后你就可以安全地完成你刚刚写的东西了
if ($stringB > stringA) {
echo "string B is still in the future!";
}
希望我的帖子能帮你摆脱困境希望我的帖子能帮你摆脱困境