[英]How do you convert a time “duration” from a string value into an integer?
I have a bunch of string values that represent an interval in time like this: 我有一堆代表时间间隔的字符串值,如下所示:
'1 hour'
, '1.5 hours'
, '30 minutes'
, etc... '1 hour'
, '1.5 hours'
, '30 minutes'
等。
I'd like to convert all of these values into a count of minutes, so the above would become respectively: 我想将所有这些值转换为分钟数,因此上述内容将分别变为:
60
, 90
, 30
60
, 90
, 30
I've been messing around with Carbon\\Carbon
and PHP's strtotime()
function, but I can't figure out how I'd do this. 我一直在弄混
Carbon\\Carbon
和PHP的strtotime()
函数,但是我不知道该怎么做。 It's probably really simple? 这可能真的很简单吗?
$str = '1 hour';
$int1 = strtotime("+".$str)- strtotime("now");
Here $int1
will return time in seconds. $int1
将以秒为单位返回时间。 here it will return 3600
. 在这里它将返回
3600
。 And this wont handle fractions like 1.5 hours
etc. 这将无法处理
1.5 hours
类的分数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.