intersection in hours/mins of two timestamps
How can one achieve the intersection in hours/min between two timestamps?
t1 = 1970-01-01 23:00:00
t2 = 1970-01-02 11:00:00
d1 = 1970-01-01 22:30:00
d2 = 1970-01-02 01:00:00
intersection = 2h:00mins
a开发者_如何转开发 different example would be:
t1 = 1970-01-01 23:00:00
t2 = 1970-01-02 11:00:00
d1 = 1970-01-01 08:00:00
d2 = 1970-01-01 11:30:00
intersection = 3hrs:00minutes
Use two DateTime objects to represent your two dates. Then, you can use the diff method to produce a DateInterval object, which in turn has the data you want.
If you don't have PHP > 5.3, something like the following is needed. It's not pretty, but if you want hours and minutes you could subtract the two timestamps and divide out the difference. $past
is a flag if you want to say something like 2h:00m ago
$difference = strtotime($d1) - strtotime($d2);
$past = $difference < 0;
$difference = abs($difference);
$interval_hours = (int)($difference / 3600);
$interval_minutes = (int)(($difference % 3600) / 60);
精彩评论