可能重复:
PHP:find day difference between two date(“YmdHis”) reture
我有两个约会
$departure_Dtae=2012-07-25T07:30:00
$arrival_date =2012-07-25T10:25:00
T表示时间,即2012-07-25T10:25:00
日期为2012-07-25
,时间为10:25:00
(hr:mts:s)
我需要找出这两次之间的总hrs 即在这种情况下,总小时为2小时55分钟 但我不知道我是如何在PHP中计算的 有谁知道这个?
答案 0 :(得分:3)
如果使用PHP Version 5.3+,那么您可以使用DateTime :: diff():
<?php
function getDiff($strStart, $strEnd) {
$start = DateTime::createFromFormat("Y-m-d G:i:s", $strStart);
$end = DateTime::createFromFormat("Y-m-d G:i:s", $strEnd);
return $start->diff($end)->format('%hhrs %imins and %sseconds ');
}
var_dump(getDiff('2012-07-25 07:30:00', '2012-07-25 10:25:00'));
答案 1 :(得分:1)
its simple
$departure_Dtae="2012-07-25T07:30:00";
$arrival_date ="2012-07-25T10:25:00";
$departure_Dtae= str_replace("T", " ", $departure_Dtae);
$arrival_date= str_replace("T", " ", $arrival_date);
$diff= (strtotime($arrival_date)-strtotime($departure_Dtae));
echo date("h",$diff);
答案 2 :(得分:0)
试试这个:
$end_time = "2008-09-05 20:59:13";
$start_time = "2008-09-05 19:00:16";
$end = date("h:i:s",strtotime($end_time));
$start = date("h:i:s",strtotime($start_time));
$diff = strtotime($end) - strtotime($start);
//convert to min and sec
$convert_min = $diff/60;
$convert_sec = $diff % 60;//seconds
//convert to hours and min
$convert_hr = floor($convert_min/60);//hours
$remainder = floor($convert_min % 60);//minutes
$total_visit = $convert_hr.":".$remainder;
答案 3 :(得分:0)
您可以在php中使用DateTime对象,该对象具有多种操作日期的方法。
DateTime :: createFromFormat http://www.php.net/manual/en/datetime.createfromformat.php和DateTime :: diff http://www.php.net/manual/en/datetime.diff.php将是执行此任务所需的函数。
$date1 = DateTime::createFromFormat('j-M-Y H:i', '15-Feb-2009 12:45');
$date2 = DateTime::createFromFormat('j-M-Y H:i', '15-Feb-2009 13:45');
$interval = $date1->diff($date2);
echo $interval->format('H:i');
答案 4 :(得分:0)
strtotime(date($arrival_date)) - strtotime(date($departure_date));
将以秒为单位给你时间差异,然后你可以按照自己的意愿操纵它。