如何在php/mysql中比较日期和时间
问题描述:
我需要比较php/mysql中的日期和时间,基本上我有一个应用程序和一个服务器,该应用程序需要连接到服务器以检查进入数据库的新条目.服务器从应用程序接收日期时间作为字符串,在这里完成
i need to compare date and time in php/mysql basically i have an application and a server the application needs to connect to the server to check new entries into the database. the server receives the datetime as a string from the application which is done here
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));
并且需要在此处进行比较
and the comparison needs to be done here
if ($dbMax_time> $timestr) {
$NewRooms = mysql_query("SELECT * From room WHERE created_at> CAST($timestr AS TIME)");
}
我该怎么做,在比较上感到困惑,请帮助我修复它.
how do i go about this am confused on how to compare please help me to FIX it.
答
只需比较两个时间戳即可:
Just comparing two timestamps would suffice:
$t1 = strtotime($time);
$t2 = strtotime($Max_Time);
if($t1 > $t2) { .. }