我需要有关此代码的帮助。我试图在我的数据库中的时间和48小时之间获得时间差。我想要的是数据记录到我的数据库和48小时之前的时间差异。所以这就像是48小时前的倒计时。
例如,我在2017-10-18 02:49:25的数据库中将数据记录到我的数据库中。
有时它会比预期的结果低2小时。
$date = '2017-10-18 02:49:25';
$date1 = new DateTime($date.' A');
$date1->modify('+2 day'); //am trying to get 48 hours ahead of the time in my DB
$tomorrowDATE = $date1->format('Y-m-d h:i:s A');
$starttimestamp = strtotime($date);
$endtimestamp = strtotime($tomorrowDATE);
//Here am getting time difference in hours between when the data was logged
into my DB and current time of the day
$currenttime = round(abs(strtotime(date('Y-m-d h:i:s A')) - $starttimestamp)/3600);
$difference = round(abs($endtimestamp - $starttimestamp)/3600);
print $difference - $currenttime . 'hour left';
答案 0 :(得分:0)
您将使用mysql查询获得时差 例如。
$set_waiting_time_val ="set time"; like as current time ,now() SELECT user_lastlogin, TIMEDIFF( user_lastlogin,$set_waiting_time_val ) AS last_login_time FROM user_login
您还可以转换为小时
SELECT user_lastlogin, TIME_FORMAT( ABS( TIMEDIFF( user_lastlogin, now( ) ) ) , '%H hours' ) AS last_login_time
FROM user_login
答案 1 :(得分:0)
您开始使用DateTime,但后来又恢复使用它。您可以将DateTime用于整个事物:
<?php
# Change accordingly
date_default_timezone_set('America/Montreal');
# Time in database
$readfromdb = '2017-10-24 02:49:25 A';
$datedb = new DateTime($readfromdb);
$datedbplus48 = new DateTime($readfromdb);
$datedbplus48->modify('+2 day');
$datenow = new DateTime(); # default to now
$difference = $datedbplus48->diff($datenow);
echo "<pre>\n";
echo "DATEDB ";
echo $datedb->format('Y-m-d h:i:s');
echo "\nDATEPLUS48 ";
echo $datedbplus48->format('Y-m-d h:i:s');
echo "\nDATENOW ";
echo $datenow->format('Y-m-d h:i:s');
echo "\nDIFF ";
echo $difference->days * 24 + $difference->format('%H');
echo "</pre>\n";
?>
我认为:
结果:
DATEDB 2017-10-24 02:49:25 DATEPLUS48 2017-10-26 02:49:25 DATENOW 2017-10-24 09:03:40 DIFF 36