两个时间戳的小时/分钟交叉点

时间:2011-04-22 02:36:48

标签: php

如何在两个时间戳之间实现以小时/分钟为单位的交叉点?

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

另一个例子是:

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

1 个答案:

答案 0 :(得分:0)

如果您没有PHP> 5.3,需要类似以下内容。它不漂亮,但如果你想要几小时和几分钟,你可以减去两个时间戳并分出差异。如果您想说$past

之类的话,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);