我的数据库中有4列,它们是:
Date_in | Time_in | Date_out | Time_out
Date_in和Time_in属于一起(例如,2013-02-18 13:00:00),Date_out与Time_out一起。我想找出和之间的区别,我一直到:
$start_time = new DateTime("'$list[date_in] "."$list[time_in]'");
$since_start = $start_time->diff(new DateTime("'$list[date_out] "."$list[time_out]'"));
$hours = $since_start->h.' hours';
但它不起作用。我想我的引号和双引号都乱了,因为"'.的使用真的让我很困惑。
提前感谢您对我如何修复代码的任何建议!
[编辑]感谢大家的详细帮助!我刚刚意识到服务器不支持php5.3,我不能使用datetime()。
所以我的解决方案是:
$start_time = strtotime("$list[date_in] " . "$list[time_in]");
$end_time = strtotime("$list[date_out] " . "$list[time_out]");
$hours = abs(($end_time - $start_time)/3600);
试试这个,
function datediff( $date1, $date2 )
{
$diff = abs( strtotime( $date1 ) - strtotime( $date2 ) );
return sprintf
(
"%d Days, %d Hours, %d Mins, %d Seconds",
intval( $diff / 86400 ),
intval( ( $diff % 86400 ) / 3600),
intval( ( $diff / 60 ) % 60 ),
intval( $diff % 60 )
);
}
print datediff( "18th February 2013", "now" ) . "n";
或
您可以使用DateTime::diff
$start_date = new DateTime("2012-02-10 11:26:00");
$end_date = new DateTime("2012-04-25 01:50:00");
$interval = $start_date->diff($end_date);
echo "Result " . $interval->y . " years, " . $interval->m." months, ".$interval->d." days ";
编辑:
结账链接,
如何使用PHP计算两个日期之间的差异?
Php日期时间–计算两个日期之间差异的7种方法。
可能对你有帮助。
如果要评估变量内容,则不需要引号。在这种情况下,您只需要它们来创建日期和时间之间的分隔:
$start_time = new DateTime($list['date_in']." ".$list['time_in']);
$since_start = $start_time->diff(new DateTime($list['date_out']." ".$list['time_out']));
我使用点.
将变量与字符串连接起来,在这种情况下,字符串是空白。
您可以在文档中阅读有关串联的更多信息。
$start_time = new DateTime("$list[date_in] " . "$list[time_in]");
$since_start = $start_time->diff(new DateTime("$list[date_out] " . "$list[time_out]"));
或
$start_time = new DateTime("{$list['date_in']} {$list['time_in']}");
$since_start = $start_time->diff(new DateTime("{$list['date_out']} {$list['time_out']}"));
// Time format is UNIX timestamp or
// PHP strtotime compatible strings
function dateDiff($time1, $time2, $precision = 6) {
// If not numeric then convert texts to unix timestamps
if (!is_int($time1)) {
$time1 = strtotime($time1);
}
if (!is_int($time2)) {
$time2 = strtotime($time2);
}
// If time1 is bigger than time2
// Then swap time1 and time2
if ($time1 > $time2) {
$ttime = $time1;
$time1 = $time2;
$time2 = $ttime;
}
// Set up intervals and diffs arrays
$intervals = array('year','month','day','hour','minute','second');
$diffs = array();
// Loop thru all intervals
foreach ($intervals as $interval) {
// Set default diff to 0
$diffs[$interval] = 0;
// Create temp time from time1 and interval
$ttime = strtotime("+1 " . $interval, $time1);
// Loop until temp time is smaller than time2
while ($time2 >= $ttime) {
$time1 = $ttime;
$diffs[$interval]++;
// Create new temp time from time1 and interval
$ttime = strtotime("+1 " . $interval, $time1);
}
}
$count = 0;
$times = array();
// Loop thru all diffs
foreach ($diffs as $interval => $value) {
// Break if we have needed precission
if ($count >= $precision) {
break;
}
// Add value and interval
// if value is bigger than 0
if ($value > 0) {
// Add s if value is not 1
if ($value != 1) {
$interval .= "s";
}
// Add value and interval to times array
$times[] = $value . " " . $interval;
$count++;
}
}
// Return string with times
return implode(", ", $times);
}
// Set start & end time
$start_time = "2013-02-18 13:00:00";
$end_time = "2013-02-16 10:00:00";
// Run and print diff
echo dateDiff($start_time, $end_time, 6);
最后一个论点是精确度。
$diff = abs( strtotime( '2014-04-25 16:00:00' ) - strtotime( '2014-04-27 18:02:00' ) );
if(sprintf("%d",intval( $diff / 86400 )) != '0'){
if(sprintf("%d",intval( $diff / 86400 )) == '1'){
echo sprintf("%02d day ", intval( $diff / 86400 ));
}else{
echo sprintf("%02d days ", intval( $diff / 86400 ));
}
}
if(intval( ( $diff % 86400 ) / 3600) != '0'){
if(intval( ( $diff % 86400 ) / 3600) == '1'){
echo sprintf("%02d hour ", intval( ( $diff % 86400 ) / 3600));
}else{
echo sprintf("%02d hours ", intval( ( $diff % 86400 ) / 3600));
}
}
if(intval( ( $diff / 60 ) % 60 ) != '0'){
if(intval( ( $diff / 60 ) % 60 ) == '1'){
echo sprintf("%02d min", intval( ( $diff / 60 ) % 60 ));
}else{
echo sprintf("%02d mins", intval( ( $diff / 60 ) % 60 ));
}
}