PHP 日期比较和 strtotime
PHP date comparison and strtotime
使用 WIndows、XAMPP 5.6.8 我正在 HTML 和 PHP 中构建一个简单的网络应用程序。
我想将从数据库中检索到的日期与今天的日期进行比较。
我有一个函数可以成功 returns 一个字符串值(英国日期格式 d-m-y
)。
到目前为止我的代码是;
$expDate = get_api_data($id); // returns a string
var_dump($expDate); // this prints string(8) "31-12-19"
使用这个 $expDate
值我想实现类似的东西;
if (strtotime('d-m-y', $expDate) > time()) { // if date > than today
echo 'date is greater than today';
}
elseif (strtotime('d-m-y', $expDate) < time()) { // if date < than today
echo 'date is less than today';
}
else {
echo 'date not found';
}
目前我收到日期小于今天 - 尽管日期是31-12-19
。我不确定我是否以正确的方式处理这个问题?
任何帮助将不胜感激,因为我已经花了很多时间研究无济于事的答案。
我在执行你的代码时遇到这个错误
PHP Notice: A non well formed numeric value encountered
您应该查看文档以便更好地使用此功能:http://php.net/manual/fr/function.strtotime.php
第一个参数应该是时间,而不是格式。
顺便说一下,我更喜欢使用 DateTime class 来比较日期,你可以这样做:
<?php
$expectedDate = \DateTime::createFromFormat('d-m-y', get_api_data($id));
$nowDate = new \DateTime();
if ($expectedDate > $nowDate) { // if date > than today
echo 'date is greater than today';
}
elseif ($expectedDate < $nowDate) { // if date < than today
echo 'date is less than today';
}
else {
echo 'date not found';
}
$formattedDate = DateTime::createFromFormat('d-m-y', $expDate);
$expDate= $formattedDate->getTimestamp();
if ($expDate > time()) { // if date > than today
echo 'date is greater than today';
.....
尝试上面的代码示例,如果您有 PHP 5.2.0 或更高版本 http://php.net/manual/en/datetime.createfromformat.php,请尝试使用 DateTime class。然后使用该 dateTime 对象,您可以按照您想要的方式进行比较,例如在我的样本中,我是按时间做的。
您的代码将显示一条通知。如果您打开 php 错误报告,您将观察到它。
使用 WIndows、XAMPP 5.6.8 我正在 HTML 和 PHP 中构建一个简单的网络应用程序。
我想将从数据库中检索到的日期与今天的日期进行比较。
我有一个函数可以成功 returns 一个字符串值(英国日期格式 d-m-y
)。
到目前为止我的代码是;
$expDate = get_api_data($id); // returns a string
var_dump($expDate); // this prints string(8) "31-12-19"
使用这个 $expDate
值我想实现类似的东西;
if (strtotime('d-m-y', $expDate) > time()) { // if date > than today
echo 'date is greater than today';
}
elseif (strtotime('d-m-y', $expDate) < time()) { // if date < than today
echo 'date is less than today';
}
else {
echo 'date not found';
}
目前我收到日期小于今天 - 尽管日期是31-12-19
。我不确定我是否以正确的方式处理这个问题?
任何帮助将不胜感激,因为我已经花了很多时间研究无济于事的答案。
我在执行你的代码时遇到这个错误
PHP Notice: A non well formed numeric value encountered
您应该查看文档以便更好地使用此功能:http://php.net/manual/fr/function.strtotime.php
第一个参数应该是时间,而不是格式。
顺便说一下,我更喜欢使用 DateTime class 来比较日期,你可以这样做:
<?php
$expectedDate = \DateTime::createFromFormat('d-m-y', get_api_data($id));
$nowDate = new \DateTime();
if ($expectedDate > $nowDate) { // if date > than today
echo 'date is greater than today';
}
elseif ($expectedDate < $nowDate) { // if date < than today
echo 'date is less than today';
}
else {
echo 'date not found';
}
$formattedDate = DateTime::createFromFormat('d-m-y', $expDate);
$expDate= $formattedDate->getTimestamp();
if ($expDate > time()) { // if date > than today
echo 'date is greater than today';
.....
尝试上面的代码示例,如果您有 PHP 5.2.0 或更高版本 http://php.net/manual/en/datetime.createfromformat.php,请尝试使用 DateTime class。然后使用该 dateTime 对象,您可以按照您想要的方式进行比较,例如在我的样本中,我是按时间做的。
您的代码将显示一条通知。如果您打开 php 错误报告,您将观察到它。