[php时间转换成时间戳]php时间转换成秒数的例子

更新时间:2020-04-05    来源:php函数    手机版     字体:

【www.bbyears.com--php函数】


把HH:MM:SS格式的时间字符串转换成秒数,可以使用date_parse函数解析具体的时间信息。

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

更详细的例子

转换成多少天/多少小时/多少分

function get_stay_time($timestamp, $is_hour = 1, $is_minutes = 1)
{
    $CI =& get_instance();

    if(empty($timestamp) || $timestamp <= 60) {
        return false;
    }

    $time = time();
    $remain_time = $time - $timestamp;

    $day = floor($remain_time / (3600*24));
    $day = $day > 0 ? $day."天" : "";
    $hour = floor(($remain_time % (3600*24)) / 3600);
    $hour = $hour > 0 ? $hour."小时" : "";
    if($is_hour && $is_minutes) {
        $minutes = floor((($remain_time % (3600*24)) % 3600) / 60);
        $minutes = $minutes > 0 ? $minutes."分" : "";
        return $day.$hour.$minutes;
    }

    if($hour) {
        return $day.$hour;
    }
    return $day;
}

本文来源:http://www.bbyears.com/jiaocheng/92047.html