将24小时时间转换为秒的最佳方法是什么,以便可以用于比较if语句..
function HourMinuteToDecimal($hour_minute) {
$t = explode(':', $hour_minute);
return $t[0] * 60 + $t[1];
}
echo HourMinuteToDecimal("23:30");
return 1410
Run Code Online (Sandbox Code Playgroud)
如果您尝试将午夜时间(00:00)转换为秒,则无效.这是什么解决方案?
00:00,00:30,01:00,01:30等
if (HourMinuteToDecimal("01:30") > HourMinuteToDecimal("23:30")) { .. }
Run Code Online (Sandbox Code Playgroud)
这不行.
Fed*_*TIK 10
PHP5.3
$formattedTime = '00:01:38';
$seconds = strtotime('1970-01-01 ' . $formattedTime . 'GMT')
Run Code Online (Sandbox Code Playgroud)
转换功能:
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'];
}
Run Code Online (Sandbox Code Playgroud)
即时写入,未经测试:-P
所以,您可以使用strtotime转换unix时间戳和隔离区中的格式日期,使用标准运算符(== <>> = = =!=等)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.
Run Code Online (Sandbox Code Playgroud)
等等..
| 归档时间: |
|
| 查看次数: |
28292 次 |
| 最近记录: |