我正在为一家公司开发旅游门户网站,但很难找到时差。
使用相同的存储日期的日期时间格式的上午/下午我必须…
- 查找正在运行的出租车
- 使用这些相同的日期,我必须计算旅行开始和结束之间的时间差
- 最后,还有多少时间开始旅行
是的,还有一件事;由于这家旅游公司在印度运营,并遵循12小时的时间格式,我更难计算时差。
请使用下面的date_diff函数,它也允许上午/下午。
print_r( date_diff( date_create('2015-10-04 01:00:00am'), date_create('2015-10-04 02:00:00pm')) );
输出-->
DateInterval Object
(
[y] => 0 // corresponds to years
[m] => 0 // corresponds to months
[d] => 0 // corresponds to days
[h] => 13 // corresponds to hours
[i] => 0 // corresponds to minutes
[s] => 0 // corresponds to seconds
[weekday] => 0
[weekday_behavior] => 0
[first_last_day_of] => 0
[invert] => 0
[days] => 0
[special_type] => 0
[special_amount] => 0
[have_weekday_relative] => 0
[have_special_relative] => 0
)
从DateInterval对象可以获得时间差。
$datetime_diff_Obj = date_diff( date_create('2015-10-04 01:00:00am'), date_create('2015-10-04 02:00:00pm'));
echo $datetime_diff_Obj->h; // hours
echo $datetime_diff_Obj->i ;// minutes
echo $datetime_diff_Obj->s; // seconds
要获得小时差异,请使用以下代码
$time_diff = (round ( abs ( strtotime('2015-10-04 01:00:00am') - strtotime('2015-10-02 02:00:00pm') ) /3600 ) );
echo $time_diff." hrs ";
输出->
35 hrs