我在数据库中有time data
,我正在数据库上使用time()
函数(值1551866538)记录时间,并且我想将php的剩余时间写为1天,但为24小时。我尝试使用date_diff()
函数,但是每次都会出错,谢谢您的帮助。
$mysqltime = $data->registerdate; // 1551866538
$now = time();
$remainingtime = $now - $mysqltime; // I want show as hour
答案 0 :(得分:0)
$mysqltime = $data->registerdate; // 1551866538
$now = time();
$remainingtime = $now - $mysqltime;
echo $hours = round($remainingtime / (60 * 60)); // hours
答案 1 :(得分:0)
<?php
// Declare and define two dates
$date1 = strtotime("2016-06-01 22:45:00");
$date2 = strtotime("2018-09-21 10:44:01");
// Formulate the Difference between two dates
$diff = abs($date2 - $date1);
// To get the year divide the resultant date into
// total seconds in a year (365*60*60*24)
$years = floor($diff / (365*60*60*24));
// To get the month, subtract it with years and
// divide the resultant date into
// total seconds in a month (30*60*60*24)
$months = floor(($diff - $years * 365*60*60*24)
/ (30*60*60*24));
// To get the day, subtract it with years and
// months and divide the resultant date into
// total seconds in a days (60*60*24)
$days = floor(($diff - $years * 365*60*60*24 -
$months*30*60*60*24)/ (60*60*24));
// To get the hour, subtract it with years,
// months & seconds and divide the resultant
// date into total seconds in a hours (60*60)
$hours = floor(($diff - $years * 365*60*60*24
- $months*30*60*60*24 - $days*60*60*24)
/ (60*60));
// To get the minutes, subtract it with years,
// months, seconds and hours and divide the
// resultant date into total seconds i.e. 60
$minutes = floor(($diff - $years * 365*60*60*24
- $months*30*60*60*24 - $days*60*60*24
- $hours*60*60)/ 60);
// To get the minutes, subtract it with years,
// months, seconds, hours and minutes
$seconds = floor(($diff - $years * 365*60*60*24
- $months*30*60*60*24 - $days*60*60*24
- $hours*60*60 - $minutes*60));
// Print the result
printf("%d years, %d months, %d days, %d hours, "
. "%d minutes, %d seconds", $years, $months,
$days, $hours, $minutes, $seconds);
?>
输出:
2 years, 3 months, 21 days, 11 hours, 59 minutes, 1 seconds
答案 2 :(得分:0)
您还可以如下使用DateTime
类:
$today = new DateTime('now');
$tomorrow = new DateTime(date('Y-m-d H:i:s', '1551866538'));
$difference = $today->diff($tomorrow);
echo $difference->format('%h hours %i minutes remaining');
输出
1 hours 42 minutes remaining