菲律宾语|数组与时间


PHP| arrays with times

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

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

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

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

我不确定如何,但也许将 DBtime 转换为 Unixtime:

(CurrentUnixTime - dbUnixTime) = x

或者,我们可以采用 17:38 并将其与 date("G:i") 进行比较;

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

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

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

这将以秒为单位为您提供差异。

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

time() - strtotime($DBtime)
$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');
$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
}