开发者

Time string to total minutes integer in PHP

开发者 https://www.devze.com 2023-04-04 21:24 出处:网络
Simple question, but can\'t find it elsewhere: How can I transform my string of time e.g. \"09:30\" into in integer containing the total minutes in that time? (Solution for \"09:30\" would be 570)

Simple question, but can't find it elsewhere:

How can I transform my string of time e.g. "09:30" into in integer containing the total minutes in that time? (Solution for "09:30" would be 570)

I have tried a lot of stuff, including substr开发者_如何学Python, mktime and strtottime and just can't get it to work.


$time = explode(":","09:30");
$minutes = intval($time[0])*60 + intval($time[1]);


Just for reference, strtotime works as well and it may be a simpler solution.

$minutes = strtotime('09:30', 0) / 60;


Try to explode the time string (something like: explode(":","09:30")). You can also use str_replace. Then just simply multiply the 09 with 60 and add the 30 to it.

Hope it helps!

0

精彩评论

暂无评论...
验证码 换一张
取 消