简体   繁体   English

比较PHP中的MySQL日期

[英]Comparing mysql dates in php

I'm creating an application for a friend to handle Deadlines. 我正在为朋友创建一个处理截止日期的应用程序。 I have a page set up where each user can see their own 'jobs to do' with a deadline next to it. 我设置了一个页面,每个用户都可以看到自己的“工作要做”,并且旁边有一个截止日期。 My question is... 我的问题是

How do I compare a deadline date that comes back from the mysql query as 2010.08.08 with today's date? 如何比较从mysql查询返回的截止日期为2010.08.08和今天的日期?

For Example... 例如...

 <?php
while($row = mysql_fetch_array($result)){

$jobfinishdate = $row['jobfinishdate'];
$date = date('Y-m-d');

if ($jobfinishdate>$date)
 $jobfinishdate .= " - Not due yet!" ;
 else if ($jobfinishdate<$date)
  $jobfinishdate .= " - You didn't meet this deadline!";
  else if ($jobfinishdate==$date)
 $jobfinishdate .= " - Deadline is TODAY!";

} ?> }?>

This works ok. 这样就可以了。 But what I'd really like to do is display a message saying 'you have 5 days until deadline. 但是,我真正想做的是显示一条消息,说“您有5天的截止日期。 Any ideas how to get around this? 任何想法如何解决这个问题?

Thanks. 谢谢。

Shane. 巴蒂尔。

$days = (strtotime($jobfinishdate) - strtotime($date)) / (60 * 60 * 24);

Should get you the amount of days left on the deadline. 应该让您在截止日期前剩下的天数。

Edit: The above will always return the difference in days - to handle whether or not its before or beyond the due date, maybe (using just time() as Adam suggested): 编辑:上面的方法将始终返回以天为单位的差额-处理是否在到期日之前或之后(可能(如Adam所建议的那样仅使用time() )):

$date_passed = false;
$days = strtotime($jobfinishdate) - time();
if ($days < 0) { $date_passed = true; }
$days = $days / (60 * 60 * 24);

if (!$date_passed)
{
  echo "You have $days days left on this project.";
}
else
{
  echo "Deadline has expired $days days ago.";
}
// calculate days left
$daysleft = round( ( strtotime( $jobfinishdate ) - time() ) / 86400 );

// print out text for $daysleft
if( $daysleft == 0 )
   print( "Deadline is today!" );
else if ( $daysleft > 0 )
   printf( "You have %d days until deadline.", $daysleft );
else
   print( "You did not meet the deadline!" );

If possible I would let the database return the number of days, with a query like this: 如果可能的话,我会让数据库返回天数,如下所示:

SELECT jobfinishdate, datediff(jobfinishdate, NOW() AS days) FROM table

Then use this number of days: 然后使用以下天数:

while ($row = mysql_fetch_array($result)){
    $jobfinishdate = $row['jobfinishdate'];
    $days = $row['days'];   

    if ($days > 0) {
        $jobfinishdate .= " - Not due yet! $days until deadline" ;
    } elseif ($days < 0) {
        $jobfinishdate .= " - You didn't meet this deadline!";
    } else {
        $jobfinishdate .= " - Deadline is TODAY!";
}

} }

Some other remarks: 其他说明:

  • If you keep the date calculation in PHP, move the $date declaration outside the while loop because you only need to do that once. 如果将日期计算保持在PHP中,请将$ date声明移到while循环之外,因为您只需执行一次即可。
  • you can remove the last condition, if ($jobfinishdate==$date). 如果($ jobfinishdate == $ date),则可以删除最后一个条件。 If the date is not larger and not smaller, it can only be equal. 如果日期不大也不小,则日期只能相等。

如果您使用的是PHP 5.3.0或更高版本,则可以使用DateTime对象

In SQL query: 在SQL查询中:

SELECT
...,
TIMESTAMPDIFF(DAY, NOW(), `date_deadline`) AS days_to_deadline
...

This will produce positive number of days due deadline and negative for expired tasks. 这将在截止日期前产生正数天,而对于到期的任务将产生负数。

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

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