How to convert a “HH:MM:SS” string to seconds with PHP?

后端 未结 4 739
盖世英雄少女心
盖世英雄少女心 2020-12-18 01:20

Is there a native way of doing \"HH:MM:SS\" to seconds with PHP 5.3 rather than doing a split on the colon\'s and multipling out each section the relevant numbe

4条回答
  •  伪装坚强ぢ
    2020-12-18 01:23

    I think the easiest method would be to use strtotime() function:

    $time = '21:30:10';
    $seconds = strtotime("1970-01-01 $time UTC");
    echo $seconds;
    

    demo


    Function date_parse() can also be used for parsing date and time:

    $time = '21:30:10';
    $parsed = date_parse($time);
    $seconds = $parsed['hour'] * 3600 + $parsed['minute'] * 60 + $parsed['second'];
    

    demo

提交回复
热议问题