-1

Possible Duplicate:
get difference in time in HH:MM format php

my time format is d/m/Y - h:ia

example output is 21/10/2012 - 03:49pm

I'm using this code to calculate the difference between $from and $to

 $to = strtotime("21/10/2012 - 10:30pm");
 $from = strtotime("22/10/2012 - 8:30am");
 $stat = date("H\h:i\m", abs($to - $from));

but I'm alwys getting an incorrect value

for the example above I got an output of 16h:00m but it should be 10h:00m

is there a much more efficient and effective way to do this?

Community
  • 1
  • 1
telexper
  • 2,381
  • 8
  • 37
  • 66

2 Answers2

1

This format is incorrect and unrecognized by strtotime (see the third note) I suspect that your timezone is GMT-8 and you will always get 16h from dates in this format.

You have to convert manually this date to format recognized by php in order to do some calculations

dev-null-dweller
  • 29,274
  • 3
  • 65
  • 85
1

logout:

if(function_exists('date_default_timezone_set')) date_default_timezone_set('America/New_York');

$from = $_SESSION['loginT'];
$to = date('m/d/Y  h:i A');
$diff_seconds  = strtotime($to) - strtotime($from);
$stat = floor($diff_seconds/3600).'H:'.floor(($diff_seconds%3600)/60).'M';

from login:

$_SESSION['loginT'] = date('m/d/Y  h:i A');

sample output:

21/10/2012 - 06:37pm    21/11/2012 - 06:07pm        23H:30M
Community
  • 1
  • 1
telexper
  • 2,381
  • 8
  • 37
  • 66