简体   繁体   English

日期比较PHP Mysql

[英]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: 我有一个将mysql表日期与今天的日期进行比较的系统,以查看是否需要续订某些文档:

<?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:1-> iep_renewal:12-02-2012

id: 2 -> iep_renewal: 12-08-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) $ today = 2012年2月19日(今天的日期)和$ redate_converted_date = 12-02-2012年(数据库的日期)

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. 使用strtotime()DateTime将它们转换为可以比较的时间戳(或DateTime对象)。

As another possibility, you can export as a unix time stamp straight out of the DB by wrapping it in a function like this: 另一种可能性是,可以通过将其包装在以下函数中,将其直接作为unix时间戳导出到数据库中:

SELECT *, UNIX_TIMESTAMP(`iep_renewal`) AS iep_renewal_unix

There may be performance issues associated with wrapping columns in functions on large data sets. 在大型数据集的函数中包装列可能会导致性能问题。

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

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