简体   繁体   English

PHP检查日期是否已过(不工作 - 为什么?)

[英]PHP Check if date has passed (Not Working - Why?)

I have just realised that when I validate a code, it shows valid all the time, even when the expiry date is old. 我刚刚意识到,当我验证代码时,它始终显示有效,即使有效期已过。 Can someone see what I got wrong. 有人能看出我错了。

Just not getting it... 只是没有得到它......

{
    $status = "<p8>ERROR</p8>";
    $entity_name = $rowa['entity_name'];
    $cert_no = $rowa['cert_no'];
    $issue_num = $rowa['issue_num'];
    $level = $rowa['level'];
    //$issue_date = $rowa['issue_date'];
    $issue_date = date("d-m-Y", strtotime($rowa['issue_date']));
    //$expiry_date = $rowa['expiry_date'];
    $expiry_date = date("d-m-Y", strtotime($rowa['expiry_date']));
    //$status = $rowa['status'];
    $date_time = date("d-m-Y");
    if($date_time < $expiry_date)
    {
        $status = "<p8>Valid</p8>";
    }
    else
    {
        $status = "<p9>In-Valid</p9>";
    }
}

By using date you are converting the dates into strings - and because you are using a 'dm-y' format, they aren't compared in the way you think they are. 通过使用date您将日期转换为字符串 - 并且因为您使用的是'dm-y'格式,所以不会按照您认为的方式对它们进行比较。

A quick and dirty solution is to reverse the format and use 'Ymd' which will return them in format like 20130905 which WILL work as a string comparison, but the better method is to create them in an actual date time format or a timestamp via something like mktime(0, 0, 0, $month, $day, $year); 一个快速而肮脏的解决方案是反转格式并使用'Ymd' ,它将返回它们的格式,如20130905 ,它将作为字符串比较,但更好的方法是以实际的日期时间格式或时间戳通过某些东西创建它们喜欢mktime(0, 0, 0, $month, $day, $year);

转换日期以在unixtime中与mktime进行比较

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM