如何在php/mysql中比较日期和时间 [英] how to compare date and time in php/ mysql

查看:82
本文介绍了如何在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) { .. }

这篇关于如何在php/mysql中比较日期和时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆