嗨,我正在尝试制作一些比较数据库中日期的东西,以检查我的许可证是否已过期。
我的日期以这种格式存储:18/05/2019
(d/m/Y)
在我的代码中,我为今天的日期创建了一个变量:
$TodayDate = Date('d/m/Y');
然后我在我的数据库中得到包含日期的行,如下所示:
$DBLicenseExpireDate = $row["LicenseExpireDate"];
最后我尝试查看日期是否已过期:
if ($TodayDate < $DBLicenseExpireDate) {
}
我也试过:
if ($TodayDate < strtotime($DBLicenseExpireDate)) {
}
在几乎所有情况下,我都尝试过,它说它已过期,而实际上并没有。
我得到了一些奇怪的结果。示例:如果今天的日期是:05/18/2019,到期日期是:06/06/2019,我试试这个 if 语句:
if($todayDate < $expireDate)
{
echo 'not expired';
}
else
{
echo 'expired';
}
它仍然是过期的结果。看到了关于检查过期日期的数百万个问题,都试过了。我一定做错了什么。
strtotime()
用于日期数学。使用更适合处理日期的`DateTime()。strtotime()
. 当使用 m/d/Y 格式时,假设您使用的是美国日期格式,并且类似的日期18/05/2019
被评估为“2019 年第 18 个月的第五天”。显然这不是你的意思,结果你会得到1970-01-01。使用DateTime::CreateFromFormat()
得到你想要的日期,然后做你的比较:
// Parse the european date format
$date1 = DateTime::CreateFromFormat('%d/%m/%Y', '18/05/2019');
// Get today ("now")
$date2 = new DateTime();
// DateTime objects are comparable so you can compare these two variables directly
if ($date2 < $date1) {
// today is before the 18th of May
}
else {
// today is after the 18th of May
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句