0

Possible Duplicate:
PHP:find day difference between two date(“YmdHis”) reture

I have two dates

$departure_Dtae=2012-07-25T07:30:00
     $arrival_date =2012-07-25T10:25:00

T means Time i.e. in 2012-07-25T10:25:00 date is 2012-07-25 and the time is 10:25:00 (hr: mts: s)

I need to find out the total hrs between these two times I.e. in this case the total hour is 2 hr and 55 minutes But I don't know how I calculate this in PHP Does anyone know this?

Community
  • 1
  • 1
user1519718
  • 457
  • 1
  • 5
  • 12

5 Answers5

3

If using PHP Version 5.3+ then you can use 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'));
James Arnold
  • 990
  • 1
  • 6
  • 15
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);
Ravi Jethva
  • 1,931
  • 2
  • 11
  • 12
0

Try this :

$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;  
Krishna
  • 1,540
  • 2
  • 11
  • 25
0

You can use the DateTime object in php, which has loads of methods for manipulating dates.

DateTime::createFromFormat http://www.php.net/manual/en/datetime.createfromformat.php and DateTime::diff http://www.php.net/manual/en/datetime.diff.php would be the functions you would need to perform this task.

$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');
Theo Kouzelis
  • 3,195
  • 5
  • 37
  • 66
0
strtotime(date($arrival_date)) - strtotime(date($departure_date));

Will give you the time diff in secounds, then you can manipulate that as you wish.

Alex Kolarski
  • 3,255
  • 1
  • 25
  • 35