Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Datetime在Datetime之前或之后_Php_Datetime_Compare - Fatal编程技术网

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!";
    }

希望我的帖子能帮你摆脱困境希望我的帖子能帮你摆脱困境