确定经过时间 [英] Determining elapsed time

查看:105
本文介绍了确定经过时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PHP中有两次,我想确定经过的时间和分钟。例如:



8:30至10:00将为1:30

解决方案

解决方案可能是使用 strtotime

  $ first_str ='8:30'; 
$ first_ts = strtotime($ first_str);

$ second_str = '10:00';
$ second_ts = strtotime($ second_str);

然后,做出区别:

  $ difference_seconds = abs($ second_ts  -  $ first_ts); 

在几分钟或几个小时内得到结果:

  $ difference_minutes = $ difference_seconds / 60; 
$ difference_hours = $ difference_minutes / 60;
var_dump($ difference_minutes,$ difference_hours);

你会得到:

  int 90 
float 1.5

找出是如何显示的; - )





(再思考一点以后编辑)



显示差异的可能性可能是使用 date 函数;这样做应该做到:

  date_default_timezone_set('UTC'); 

$ date = date('H:i',$ difference_seconds);
var_dump($ date);

我得到:

  string '01:30'(length = 5)

在我的系统上,我不得不使用 date_default_timezone_set 将时区设置为UTC - 否则,我得到02:30而不是01:30 - 可能是因为我在法国, FR是我系统的区域设置。


I have two times in PHP and I would like to determine the elapsed hours and minutes. For instance:

8:30 to 10:00 would be 1:30

解决方案

A solution might be to use strtotime to convert your dates/times to timestamps :

$first_str = '8:30';
$first_ts = strtotime($first_str);

$second_str = '10:00';
$second_ts = strtotime($second_str);

And, then, do the difference :

$difference_seconds = abs($second_ts - $first_ts);

And get the result in minutes or hours :

$difference_minutes = $difference_seconds / 60;
$difference_hours = $difference_minutes / 60;
var_dump($difference_minutes, $difference_hours);

You'll get :

int 90
float 1.5

What you now have to find out is how to display that ;-)


(edit after thinking a bit more)

A possibility to display the difference might be using the date function ; something like this should do :

date_default_timezone_set('UTC');

$date = date('H:i', $difference_seconds);
var_dump($date);

And I'm getting :

string '01:30' (length=5)

Note that, on my system, I had to use date_default_timezone_set to set the timezone to UTC -- else, I was getting "02:30", instead of "01:30" -- probably because I'm in France, and FR is the locale of my system...

这篇关于确定经过时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆