当你在PHP中创建预订和活动安排的插件或网站时,比较日期变得很有必要。它也有许多其他用途,取决于你如何实现。例如,我们可以用它来在我们的PHP脚本中创建有时间敏感性的促销活动。
在编写程序化或面向对象风格的代码时,PHP带有专门的函数和方法,可以用来比较日期。在本教程中,我们将同时讨论它们。
在PHP中比较日期(面向对象的风格)
在PHP中有一个DateTime 类,你可以用它来创建DateTime 对象。你可以把一个有效的日期/时间字符串和一个可选的时区参数传递给它,以创建一个DateTime 对象。这里有几个例子。
<?php
$past = new DateTime("18 May 2021");
$now = new DateTime("now");
$dist_past = new DateTime("2002-09-21 18:15:00");
$dist_future = new DateTime("12-09-2036");
?>
你可以在这些DateTime 对象上使用简单的比较运算符来比较现在的日期,并检查返回值是true 还是false 。
<?php
$past = new DateTime("18 May 2021");
$now = new DateTime("now");
$dist_past = new DateTime("2002-09-21 18:15:00");
$dist_future = new DateTime("12-09-2036");
if($past < $now) {
echo 'The first date is in the past.';
}
// Output: The first date is in the past.
if($past > $dist_past) {
echo 'The third date is in the distant past.';
}
// Output: The third date is in the distant past.
if($dist_future > $now) {
echo 'The fourth date is in the future.';
}
// Output: The fourth date is in the future.
?>
PHP中的日期差异(面向对象的风格)
有时,在比较日期的时候,你可能想得到一些更多的信息。在PHP中有专门的方法和函数来计算日期之间的差异。
一旦你创建了你的DateTime 对象,你也可以在一个对象上调用diff() 方法,并把另一个日期传给它,以便计算出日期之间的差异。这将给你一个DateInterval 对象。
请记住,你调用diff() 的日期将从传递给这个方法的日期中减去。之后你可以通过使用format() 方法以任何你喜欢的方式格式化差值。这里有一些例子。
<?php
$last = new DateTime("25 Dec 2020");
$now = new DateTime("now");
$next = new DateTime("25 Dec 2021");
$days_last = $last->diff($now);
$days_next = $now->diff($next);
echo $days_last->format('%a days').' since last Christmas.';
// Output: 170 days since last Christmas.
echo $days_next->format('%a days').' to next Christmas.';
// Output: 194 days to next Christmas.
?>
你可以向format() 方法传递许多参数来控制PHP如何输出差异。在我们的例子中,我们用%r 来确定差异的相对性。一个正值意味着传递给diff() 的日期与调用它的日期相比是在未来。负值意味着传递给diff() 的日期与调用它的日期相比,是过去的。%a 字符串是用来显示天数和小时数的。你也可以使用其他字符串,如%H,%I, 和%S ,分别显示小时、分钟和秒。所有这些格式化的字符都可以在方法的文档中找到 [format()](https://www.php.net/manual/en/dateinterval.format.php)方法的文档中找到。
在PHP中比较日期(程序风格)
如果你喜欢写程序风格的代码,你可以使用一组不同的函数。在这种情况下,我们可以使用date_create() 函数,从传递的字符串中创建我们的DateTime 对象。你定义的变量仍将存储DateTime 对象,但它们不会被你明确地实例化。
由于我们仍在处理DateTime 对象,常规的比较运算符仍将允许我们比较这些日期。
<?php
$past = date_create("18 May 2021");
$now = date_create("now");
$dist_past = date_create("2002-09-21 18:15:00");
$dist_future = date_create("12-09-2036");
if($past < $now) {
echo 'The first date is in the past.';
}
// Output: The first date is in the past.
if($past > $dist_past) {
echo 'The third date is in the distant past.';
}
// Output: The third date is in the distant past.
if($dist_future > $now) {
echo 'The fourth date is in the future.';
}
// Output: The fourth date is in the future.
?>
PHP中的日期差异(程序风格)
就像DateTime 类的diff() 方法一样,有一个相应的date_create() 函数,你可以用来计算两个日期之间的差异。它接受两个参数,第一个参数中提供的日期将从第二个参数中的日期中减去。
这里有一个例子,计算自去年圣诞节以来的天数和到下一个圣诞节的天数。
<?php
$last = date_create("25 Dec 2020");
$now = date_create("now");
$next = date_create("25 Dec 2021");
$days_last = date_diff($last, $now);
$days_next = date_diff($now, $next);
echo date_interval_format($days_last, '%a days').' since last Christmas.';
// Output: 170 days since last Christmas.
echo date_interval_format($days_next, '%a days').' to next Christmas.';
// Output: 194 days to next Christmas.
?>
我们想在这两种情况下保持返回的天数为正数,所以后来的时间被作为第二个参数传递。
最后的思考
PHP提供了方便的方法和函数来比较两个日期,这取决于你的编码风格。你可以使用PHP中的diff() 方法和date_diff() 函数得到更多的信息,如两个日期之间的确切时间差。在本教程中,我试图涵盖PHP中比较日期的基础知识。你应该考虑阅读文档来学习如何用更高级的方法来格式化日期的差异。