php - 使用php从数据库中获取后如何比较日期和时间?

标签 php database date time compare

假设我让这段代码在一个循环中运行,其中 $fid 将在每一轮接收一个值:

     $result2 = mysql_query( "SELECT date_create FROM users WHERE id = $fid");

现在,date_create 将采用以下格式

2012-08-16 20:13:49

我的问题是,如何检查该日期是否早于 2012-08-31,例如 2012-08-10?

我的完整代码,以防万一有人问起。

$query = "(SELECT distinct fid FROM coin WHERE uid = 59  )";
$result = mysql_query($query);
while($rows = mysql_fetch_array($result))
{
    $fid = $rows['fid'];
    echo 'fid || '.$fid;
    $result2 = mysql_query( "SELECT id, date_create FROM users WHERE id = $fid");
     $rows2 = mysql_fetch_array($result2);

     echo "  ||   users id : " . $rows2['id']; 

     if( $rows2['date_create'] < "2012-8-31") // How do i get something like this? 
     {

          echo "  ||   date_create : " . $rows2['date_create'] ." True"; 

     }
     else
     {
           echo "  ||   date_create : " . $rows2['date_create'] . "False"; 
     }  
    l();
}

到目前为止我得到的结果是这样的:

fid || 1112 || users id : 1112 || date_create : 2012-08-16 20:13:49 false <--
fid || 1113 || users id : 1113 || date_create : 2012-08-16 20:14:11 false <--
fid || 1115 || users id : 1115 || date_create : 2012-08-16 20:14:21 false <--
fid || 1117 || users id : 1117 || date_create : 2012-08-16 20:14:38 false <--
fid || 1118 || users id : 1118 || date_create : 2012-08-16 20:15:03 false <--
fid || 1119 || users id : 1119 || date_create : 2012-08-16 20:15:10 false <-- 

n.b 如果您也能提供一种检查时间的方法,我们将不胜感激。这将是额外的 :D

最佳答案

您可以通过转换日期/时间字符串并使用 strtotime() 进行比较来完成此操作.使用该方法,您还可以调整它以比较秒数:

//this effectively checks to see that the time is greater than '2012-08-16 20:13:49'
//the total calculated time on the RHS of the >= operator is effectively '2012-08-16 20:13:49'
//hence, this will echo 'YES'

//this is of the form: hours, minutes, seconds
$time_difference = (44 * 60 * 60) + (13 * 60) + 49;

if (strtotime('2012-08-16 20:13:49') >= strtotime('2012-08-15') + $time_difference)
{
  echo 'YEAH';
}
else
{
  echo 'NO';
}

更新:

来自 strtotime() 上的 PHP 文档:

The valid range of a timestamp is typically from Fri, 13 Dec 1901 20:45:54 UTC to Tue, 19 Jan 2038 03:14:07 UTC. (These are the dates that correspond to the minimum and maximum values for a 32-bit signed integer.) Additionally, not all platforms support negative timestamps, therefore your date range may be limited to no earlier than the Unix epoch. This means that e.g. dates prior to Jan 1, 1970 will not work on Windows, some Linux distributions, and a few other operating systems. PHP 5.1.0 and newer versions overcome this limitation though.

For 64-bit versions of PHP, the valid range of a timestamp is effectively infinite, as 64 bits can represent approximately 293 billion years in either direction.

关于php - 使用php从数据库中获取后如何比较日期和时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12888777/

相关文章:

database - 如何根据内容自动调整/缩放 DBGrid(或其他类似)的列宽?

java - 为什么这个用于检索当前年份的 Scala 代码返回 "113"?

javascript - 如何使用php将mysql中选定的数据插入到mysql中的另一个表中?

mysql - 在遍历结果集时将数据插入 MySQL

php - 从数据库填充数组

java - 如何更新存储在 Assets 文件夹Android中的数据库

java - 如何一起执行2个查询?

javascript将日期和时间转换为特定格式

javascript - 仅获取昨天和明天的日期,而不获取当天的日期和时间

php - MYSQL 行数错误