php – 将时间转换为秒

什么最好的方式转换24小时的时间到秒,所以它可以用于比较if语句..
function HourMinutetoDecimal($hour_minute) {
        $t = explode(':',$hour_minute);
        return $t[0] * 60 + $t[1];
}

echo HourMinutetoDecimal("23:30");
return 1410

如果您尝试将午夜时间(00:00)转换为秒,则无法正常工作.这是什么解决方案?

00:00,00:30,01:00,01:30等

if (HourMinutetoDecimal("01:30") > HourMinutetoDecimal("23:30")) { .. }

这不行.

要转换功能
function hoursToSecods ($hour) { // $hour must be a string type: "HH:mm:ss"

    $parse = array();
    if (!preg_match ('#^(?<hours>[\d]{2}):(?<mins>[\d]{2}):(?<secs>[\d]{2})$#',$hour,$parse)) {
         // Throw error,exception,etc
         throw new RuntimeException ("Hour Format not valid");
    }

         return (int) $parse['hours'] * 3600 + (int) $parse['mins'] * 60 + (int) $parse['secs'];

}

写在飞行中,没有测试:-P

所以,你可以使用strtotime来使用一个标准运算符(==<> =< =!=等)来转换unix时间戳和comparte中的格式日期ex:

$t1 = "23:40:12";
$t2 = "17:53:04";

$h1 = strtotime("0000-00-00 $t1");
$h2 = strtotime("0000-00-00 $t2");

$h1 == $h2; // if are equals
$h1 > $h2; // if h1 is mayor at h2
$h1-$h2; // dieference in seconds,etc.

等等..

相关文章

统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...
前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...
FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...
之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...
统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...