简体   繁体   English

Mysql比较存储到今天日期的日期

[英]Mysql Comparing dates stored to today's date

I want to go through my database datesandtime and compare them to the current date and time, and say which one is bigger. 我想通过我的数据库dateandtime并将它们与当前日期和时间进行比较,并说出哪一个更大。

<?php
include_once("db.php");
$date = new DateTime();
//echo $date->format('Y-m-d H:i:s')

$query = "SELECT timedate, email  FROM mailer";
$result = mysql_query($query);
echo "<table>";
while($row = mysql_fetch_array($result)){
       $tot = $row['timedate'];
       $ema = $row['email'];
    if($tot > $date) {
    //echo "<tr><td>" .$row['timedate']."</td><td>";
    echo"database dates higher then now dates" . "<br>";
    echo "<tr><td>" .$row['email']."</td><td>" ."<br>";
   echo "<tr><td>" .$row['timedate']."</td><td>" ."<br>";
}
else {
    echo"database dates lower then now dates" . "<br>";
    echo "<tr><td>" .$row['email']."</td><td>". "<br>";
       echo "<tr><td>" .$row['timedate']."</td><td>" ."<br>";
}

}
mysql_close();
?>

Here is the test output that I get: 这是我得到的测试输出:

database dates lower then now dates
barmaleyalex@gmail.com  
2015-05-04 08:00:00 
database dates lower then now dates
vladtheimpalor25@gmail.com  
2015-05-04 00:00:00 
database dates lower then now dates
barka@gmail.com 
2015-05-30 00:00:00

As you can see the last entry is wrong, its set to may 30 so it should be higher then the now date. 你可以看到最后一个条目是错误的,它设置为可能30,所以它应该高于现在的日期。 My guess is that I'm not using the current date correctly and I need to format it. 我的猜测是我没有正确使用当前日期,我需要格式化它。

Since the timedate is datetime in PHP you can simply use strtotime 由于timedatedatetime在PHP中,你可以简单地使用的strtotime

if(strtotime($tot) > time()){

}else{

}

You should first format the database result as a date before comparing them. 您应首先将数据库结果格式化为日期,然后再进行比较。

<?php
include_once("db.php");
$date = new DateTime();
//echo $date->format('Y-m-d H:i:s')

$query = "SELECT timedate, email  FROM mailer";
$result = mysql_query($query);
echo "<table>";
while($row = mysql_fetch_array($result)){
       $tot = DateTime::createFromFormat("Y-m-d H:i:s",$row['timedate']);
       $ema = $row['email'];
    if($tot > $date) {
       echo"database dates higher then now dates" . "<br>";
       echo "<tr><td>" .$row['email']."</td><td>" ."<br>";
       echo "<tr><td>" .$row['timedate']."</td><td>" ."<br>";
    } else {
       echo"database dates lower then now dates" . "<br>";
       echo "<tr><td>" .$row['email']."</td><td>". "<br>";
       echo "<tr><td>" .$row['timedate']."</td><td>" ."<br>";
    }

}
mysql_close();
?>

You cant compare dates this way. 你不能用这种方式比较日期。 You have to convert them to timestamp first.Try with - 你必须先将它们转换为timestamp试试 -

if(strtotime($tot) > strtotime($date))

if($tot > $date) you cannot directly compare datatime value, you have to convert both of them time value first. if($tot > $date)您无法直接比较数据时间值,则必须先将它们转换为时间值。

$dateTick = strtotime($date); $totTick = strtotime($tot);

After that only compare 之后才比较

if($dateTick < $totTick)

It's arguably less error-prone and potentially more efficient to let the database do this. 它可以说更容易出错,并且可能让数据库更有效率。

SELECT timedate, (timedate < NOW()) AS earlier, email  FROM mailer;

This will return a new "column" called "earlier," generated on the fly, telling you which dates are earlier with a '1' (true) or not, with a '0' (false), or NULL if timedate were null. 这将返回一个名为“更早”的新“列”,即动生成,告诉您哪些日期早先是'1'(真)或不是,有'0'(假),如果timedate为null则为NULL 。

You can also get snazzy with a CASE expression, combining multiple logical tests to derive the new information. 您还可以使用CASE表达式获得时髦,组合多个逻辑测试以获取新信息。 These operations are typically extremely fast in MySQL. 这些操作在MySQL中通常非常快。

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

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