php 将时间转换为秒

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6047693/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:12:35  来源:igfitidea点击:

Convert time to seconds

phptime

提问by user622378

What the best way to convert 24 hours time to seconds so it can be used for comparison if statement..

将 24 小时时间转换为秒的最佳方法是什么,以便它可以用于比较 if 语句..

function HourMinuteToDecimal($hour_minute) {
        $t = explode(':', $hour_minute);
        return $t[0] * 60 + $t[1];
}

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

If you try to convert midnight time (00:00) to seconds, it will not work. What is the solution to this?

如果您尝试将午夜时间 (00:00) 转换为秒,它将不起作用。解决这个问题的方法是什么?

00:00, 00:30, 01:00, 01:30, etc.

00:00、00:30、01:00、01:30等

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

This will not work.

这是行不通的。

采纳答案by Exos

To convert function:

转换函数:

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'];

}

Write on the fly, no tested :-P

即时写入,未经测试:-P

so, you can use strtotime to convert the format date in unix timestamp and comparte using a standar operators (== < > >= <= !=, etc) ex:

因此,您可以使用 strtotime 将格式日期转换为 unix 时间戳并使用标准运算符(== < > >= <= != 等)进行比较,例如:

$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.

etc..

等等..

回答by Fedir RYKHTIK

PHP5.3

PHP5.3

$formattedTime = '00:01:38';
$seconds = strtotime('1970-01-01 ' . $formattedTime . 'GMT')

回答by Lightness Races in Orbit

You seem to be asking contradicting things.

你似乎在问自相矛盾的事情。

  • You want to convert a time in HH:MMinto seconds.
  • But then you want to "compare" the result from different times, as if they had dates attached.
  • 您想将时间转换HH:MM为秒。
  • 但是随后您想“比较”不同时间的结果,就好像它们附有日期一样。

If you want to allow HourMinuteToDecimal("01:30") > HourMinuteToDecimal("23:30")to be true without a date, then, well, any comparison will return true.

如果您想在HourMinuteToDecimal("01:30") > HourMinuteToDecimal("23:30")没有日期的情况下允许为真,那么,任何比较都将返回true.

If you want to do this properly, include the date (YYYY-MM-DD HH:MM:SS) and use strtotimein PHP to get the number of seconds since the UNIX epoch.

如果您想正确执行此操作,请包含日期 ( YYYY-MM-DD HH:MM:SS) 并strtotime在 PHP 中使用以获取自 UNIX 纪元以来的秒数。



Also, your function returns minutes, not seconds.

此外,您的函数返回分钟,而不是秒。

回答by Zach Rattner

You can compare objects of type timeusing the standard <, >, ==operators. Use strtotimeto convert the string to time, then compare.

您可以time使用标准<, >,==运算符比较类型的对象。使用strtotime的字符串转换为时间,然后进行比较。