Php 如何找出两个日期之间的差异
输出为:Php 如何找出两个日期之间的差异,php,Php,输出为: $start = "2015-01-01 10:00:00"; $end = "2015-05-05 12:06:06"; $x = strtotime($start); $y = strtotime($end); $z = abs($y - $x); $days = floor($z / (60 * 60 * 24)); $years = floor($z / (365 * 60 * 60 * 24)); $months = floor(($z - $years * 3
$start = "2015-01-01 10:00:00";
$end = "2015-05-05 12:06:06";
$x = strtotime($start);
$y = strtotime($end);
$z = abs($y - $x);
$days = floor($z / (60 * 60 * 24));
$years = floor($z / (365 * 60 * 60 * 24));
$months = floor(($z - $years * 365 * 60 * 60 * 24) / (30 * 60 * 60 * 24));
$days = floor(($z - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));
$hours = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24) / (60 * 60));
$minuts = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24 - $hours * 60 * 60) / 60);
$seconds = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24 - $days * 60 * 60 * 24 - $hours * 60 * 60 - $minuts * 60));
预期产出为:
4 month 4 days 1 hour 6 minute 6 seconds
是在PHP中处理日期的好方法:
4 month 4 days 2 hour 6 minute 6 seconds
你的代码没有错,你可能需要包括你的默认时区,就像我在非洲的即时通讯一样,所以我总是用下面的一行来设置我的时区
$start = "2015-01-01 10:00:00";
$end = "2015-05-05 12:06:06";
$d1 = new DateTime($start);
$d2 = new DateTime($end);
$iv = $d2->diff($d1);
echo $iv->format('%m month, %d days, %h hours, %i minutes, %s seconds');
您只需确定您所在的区域,然后将其分配给方法date\u default\u timezone\u set()代码>$start_date=new DateTime('2007-09-01 04:10:58');
$since_start=$start_date->diff(新日期时间('2012-09-11 10:25:00');
echo$since_start->days.“总天数
”;
echo$since_start->y.“年
”;
echo$since_start->m.“月数
”;
echo$since_start->d.“天
”;
echo$since_start->h.“小时数
”;
echo$since_start->i.“分钟
”;
echo$since_start->s.“秒数
”;
使用以下代码
$start_date = new DateTime('2007-09-01 04:10:58');
$since_start = $start_date->diff(new DateTime('2012-09-11 10:25:00'));
echo $since_start->days.' days total<br>';
echo $since_start->y.' years<br>';
echo $since_start->m.' months<br>';
echo $since_start->d.' days<br>';
echo $since_start->h.' hours<br>';
echo $since_start->i.' minutes<br>';
echo $since_start->s.' seconds<br>';
$start=date_create('2015-01-01 10:00:00');
$end=创建日期('2015-05-05 12:06:06');
$diffObj=date_diff($start,$end);
//即将到来的日子
$days=$diffObj->d;
//每月
$months=$diffObj->m;
//每年
$years=$diffObj->y;
//进站时间
$hours=$diffObj->h;
//会议记录
$minutes=$diffObj->i;
//加速秒
$seconds=$diffObj->s;
回声';
回显“”$天。”第(s)天,$几个月。”月份$年。”年、“.$hours.”小时、“.$minutes.”分钟、“.$seconds.”秒;
回声';
看看Datetime对象和(Datetime->diff())()函数..顺便说一句,小时差是正确的…如果您考虑夏季/冬季时间…对于Op,这是最直接的,但是@pavlovich可能希望编辑Op预期输出的答案。4个月4天2小时6分钟6秒只是想知道,但月%m和分钟%m的输出是否相同?;)@鸣人%i将显示分钟:p
$start_date = new DateTime('2007-09-01 04:10:58');
$since_start = $start_date->diff(new DateTime('2012-09-11 10:25:00'));
echo $since_start->days.' days total<br>';
echo $since_start->y.' years<br>';
echo $since_start->m.' months<br>';
echo $since_start->d.' days<br>';
echo $since_start->h.' hours<br>';
echo $since_start->i.' minutes<br>';
echo $since_start->s.' seconds<br>';
$start = date_create('2015-01-01 10:00:00');
$end = date_create('2015-05-05 12:06:06');
$diffObj = date_diff($start, $end);
//accesing days
$days = $diffObj->d;
//accesing months
$months = $diffObj->m;
//accesing years
$years = $diffObj->y;
//accesing hours
$hours=$diffObj->h;
//accesing minutes
$minutes=$diffObj->i;
//accesing seconds
$seconds=$diffObj->s;
echo '<center>';
echo '' . $days . ' day(s), ' . $months . ' month(s), ' . $years . 'year(s), '.$hours.' hour(s),'.$minutes.' minute(s), '.$seconds.' second(s) </b>';
echo '</center>';