简体   繁体   中英

Date Comparison PHP Mysql

I have a system that is comparing mysql tabled dates with today's date to see whether a certain document needs to be renewed:

<?php
        $student_id = $row_student['student_id'];

        $query_ieps = mysql_query("SELECT * FROM `iep` WHERE student_id = ".$student_id."") or die(mysql_error());

        $iep_count = mysql_num_rows($query_ieps);

        $row_iep = mysql_fetch_assoc($query_ieps);

        $today = date("d-m-Y");

        $iep_date = $row_iep['iep_renewal'];

        $convert_iep_date = date("d-m-Y", strtotime($iep_date));

        $redate_coverted_date = $convert_iep_date; 

        if($iep_count == 0){ $iep_status = "No IEP on record"; $iep_td_status = "grey-alert"; }
        elseif($today <= $redate_coverted_date) { $iep_status = "Due for Renewal"; $iep_td_status = "mid-alert"; }
        elseif($today >= $redate_coverted_date) { $iep_status = "Up to Date"; $iep_td_status = "green-alert"; };
?>

I have two sets of values:

id: 1 -> iep_renewal: 12-02-2012

id: 2 -> iep_renewal: 12-08-2012

However the table outputs that both these values are "Up to Date" when the first is clearly not.

Is there a way to do this at all and if so how!

$some_date_timestamp = strtotime('18-02-2011');

$current_timestamp = time();

echo ($some_date_timestamp < $current_timestamp) ? 'Old date' : 'Future date';

$today = 19-02-2012 (today's date) and $redate_converted_date = 12-02-2012 (date from DB)

You can't compare dates in that format: They'll be compared as strings, not dates, leading to incorrect results.

Use strtotime() or DateTime to convert them into timestamps (or DateTime objects) which you can compare.

As another possibility, you can export as a unix time stamp straight out of the DB by wrapping it in a function like this:

SELECT *, UNIX_TIMESTAMP(`iep_renewal`) AS iep_renewal_unix

There may be performance issues associated with wrapping columns in functions on large data sets.

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