tags:

views:

39

answers:

1

Simple, eh? -sigh-

A function. Two string arguments ( I guess it doesn't matter the type). Returning a string which is the time difference between the arguments. Think about it as a counter at your local CS internet-caffe.

function time_diff($start, $stop) {
    ...
    return $diff;
}

echo "Your time playing CS: " . time_diff('19:37', '00:05') . ". Go home!";

I tried. Just too embarrassed to say how much time I've invested in this algorithm. I can't change the format (tell me about it!).

If you happen to know a class, a file or a piece of code from the depths of the internets, I'd be happy to make use of it.

"Thanks" you very much.

+4  A: 

You can't do this correctly unless you know:

  • The timezone.
  • The day associated with those times.

Once you know that, you can do:

date_default_timezone_set('timezone here');
$seconds_diff = strtotime("2010-08-29 $end") - strtotime("2010-08-29 $start");
Artefacto
The timezone is completely redundant if you just look at two different times at the same location...
poke
@poke No, it's not. See e.g. http://codepad.viper-7.com/XpC6pL
Artefacto
Well, my assumption is that, no matter were you live, the time interval from 14:00 to 16:00 is 2 hours. So what's this got to do with time zones? What am I missing here?
nush
@nush You're missing DST transitions. The clock can go back or forward a certain amount of time (typically one hour) on timezone-specific dates.
Artefacto
You're right. Timezone != DST.
nush
@nush: In many places, you're missing two times in a year when it takes 0 or 120 minutes, respectively, to go from 2 AM to 3 AM (the exact dates and times depend on each country/state legislation; the usual way is 2:59->2:00 and 1:59->3:00). (eh, looks like it's been said already)
Piskvor
This would work unless the start date was from the prior day (eg 11pm to 1 am) -- just put yesterdays date on that one and you're good to go.
Hogan
@Hogan: Let's say it's 9:30 to 12:00. Two hours and thirty minutes, or one day, two hours and thirty minutes? Or even *n* days, two hours and thirty minutes?
Piskvor
@Piskvor : You are right -- the question seems to assume less than 24 hours -- I was making that assumption.
Hogan