简体   繁体   中英

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.

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);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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