1

I'm new to CodeIgniter & PHP.
In my database I store users status in IN time and Out Time. I want to calculate that time means, how much time a user was logged in on a particular day. The user may be logged in more than one time or logged out on a particular day.
How can I calculate the time?

Time format is=04:06:37. If the user logged in 04:06:37 and logged out 04:09:37, then the time difference is 3 min and 0 sec.

pratik
  • 983
  • 3
  • 13
  • 25

2 Answers2

3

The DateTime::diff() function does what you want.

e.g.

$login    = new DateTime('2012-09-04 14:00:00');
$logout   = new DateTime('2012-09-04 16:00:00');
$interval = $logout->diff($login);
echo $interval->format('%H hours %i minutes %s seconds');
fdomig
  • 4,417
  • 3
  • 26
  • 39
  • @JigarPandya I reverted your edit since it *is wrong* to only sum up minutes. What happens to the rest? The OP may get the rest himself. – fdomig Aug 04 '12 at 12:02
2

Convert the times to timestamps using strtotime(), then subtract to most recent time from the original time. For example;

$to_time = strtotime("04:09:37");
$from_time = strtotime("04:06:37");
$time_diff = $to_time - $from_time;
echo gmdate('H:i:s', $time_diff);
smilly92
  • 2,383
  • 1
  • 23
  • 43
  • thats rgt but in particular days user may be more then one time IN or OUT and i want whole day calculation time in a single date. – pratik Aug 04 '12 at 11:33