我需要在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)");
}
我如何解决这个问题如何比较请帮助我修复它。
答案 0 :(得分:8)
只需比较两个时间戳即可:
$t1 = strtotime($time);
$t2 = strtotime($Max_Time);
if($t1 > $t2) { .. }
答案 1 :(得分: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"