2013-05-14 59 views
3

我需要在php/mysql中比较日期和时间基本上我有一个应用程序和一个服务器,应用程序需要连接到服务器来检查数据库中的新条目。服务器接收的日期时间作为来自应用程序的字符串,在这里完成如何在php/mysql中比较日期和时间

SimpleDateFormat dateformat = new SimpleDateFormat("yyyy/mm/dd hh:mm:ss"); 
Calendar cal = Calendar.getInstance(); 
String time = dateformat.format(cal.getTime()); 

服务器这里接收数据

f (isset($_GET["time"]) || isset($_GET["user_id"])) { 
$time = $_GET['time']; 
$u_id = $_GET['user_id']; 
$timestr=date('y-m-d H:i:s',strtotime($time)); 

$result = mysql_query("SELECT MAX(created_at)from room"); 
$Max_time = mysql_result($result, $row); 
$dbMax_time = date('y-m-d H:i:s',strtotime($Max_time)); 

和比较需要在这里完成

if ($dbMax_time> $timestr) { 
    $NewRooms = mysql_query("SELECT * From room WHERE created_at> CAST($timestr AS TIME)"); 
} 

我如何去解决这个困惑如何比较请帮我修正它。

回答

8

只是比较两个时间戳就足够了:

$t1 = strtotime($time); 
$t2 = strtotime($Max_Time); 

if($t1 > $t2) { .. } 
4

为什么不创建两个DateTime对象并比较它们?

像:

$A = new DateTime(); 
$A->setTimestamp(strtotime($time)); 

$B = new DateTime(); 
$B->setTimestamp(strtotime($Max_Time)); 

if ($A > $B) echo "You use if statements to compare"