0

我需要比较从数据库中获取的时间与当前时间。

$DBtime = "2013-10-29 17:38:55";

这是数据库中数组的格式。

如何将其与当前时间进行比较?

我不知道如何,但也许将 DBtime 转换为 Unixtime:

(CurrentUnixTime - dbUnixTime) = x

或者,我们可以将17:38date("G:i");进行比较。

谢谢!我希望你明白我的意思。

4

4 回答 4

1

您可以DBtime使用 PHP 将字符串转换为 unix 时间戳strtotime。在 MySQL 中,可以UNIX_TIMESTAMP在查询列时使用。

time() - strtotime($DBtime)
于 2013-10-29T20:08:40.173 回答
1

您可以使用strtotime将其转换为 UNIX 时间戳,然后减去当前时间戳。

$DBtime = "2013-10-29 17:38:55";
$db_timestamp = strtotime($DBtime);
$now = time();
$difference = $now - $db_timestamp;
echo $difference;

这将在几秒钟内为您提供差异。

于 2013-10-29T20:08:54.770 回答
0
$date1 = new DateTime('2013-10-29 17:38:55');
$date2 = new DateTime('2013-11-29 18:28:21');
$diff = $date1->diff($date2);
echo $diff->format('%m month, %d days, %h hours, %i minutes');
于 2013-10-29T20:11:14.147 回答
0
$DBtime = "2013-10-29 17:38:55";

// Set whatever timezone was used to save the data originally
date_default_timezone_set('CST6CDT');

// Get the current date/time and format the same as your input date
$curdate=date("Y-m-d H:i:s", time());

if($DBtime == $curdate) {
    // They match, do something
} else {
    // They don't match
}
于 2013-10-29T20:13:55.900 回答