Home  >  Article  >  Backend Development  >  How to convert time into seconds in php

How to convert time into seconds in php

王林
王林Original
2020-11-03 10:37:013830browse

php method to convert time into seconds: You can use the date_parse() function to parse specific time information and return an associative array containing the specified date, thereby achieving the purpose of converting time into seconds.

How to convert time into seconds in php

Function introduction:

date_parse() function returns an associative array containing the specified date. If successful, it returns an array containing the parsed date information. Associative array, returns FALSE on failure.

(Video tutorial: php video tutorial)

Example:

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

 
更详细的例子
 
转换成多少天/多少小时/多少分
 
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;
}

Related recommendations: php training

The above is the detailed content of How to convert time into seconds in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn