Home >Backend Development >PHP Tutorial >PHP time function, ready to use

PHP time function, ready to use

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-07-29 09:08:49809browse
  • Time difference function

<code>function tranTime($time)
{
    $rtime = date("m-d H:i", $time);
    $htime = date("H:i", $time);
    $time = time() - $time;
    if ($time < 60) {
        $str = '刚刚';
    } elseif ($time < 60 * 60) {
        $min = floor($time / 60);
        $str = $min . '分钟前';
    } elseif ($time < 60 * 60 * 24) {
        $h = floor($time / (60 * 60));
        $str = $h . '小时前 ' . $htime;
    } elseif ($time < 60 * 60 * 24 * 3) {
        $d = floor($time / (60 * 60 * 24));
        if ($d == 1)
            $str = '昨天 ' . $rtime;
        else
            $str = '前天 ' . $rtime;
    } else {
        $str = $rtime;
    }
    return $str;
}
</code>
  • Countdown days hours minutes seconds function

<code>function get_countdown($unix_time = '')
  {
    if (empty($unix_time)) return false;
    if ($unix_time < time()) return array("day" => 0, "hour" => 0, "min" => 0, "sec" => 0);
    $timediff = $unix_time - time();
    $days = intval($timediff / 86400); // 时间差算整天数
    $remain = $timediff % 86400;       //整除天数取余,余下来秒数 记作 A
    $hours = intval($remain / 3600);   // 用A除3600 算整小时数
    $remain = $remain % 3600;          //用A整除小时取余数 记作B
    $mins = intval($remain / 60);      // 用B除60算整分数
    $secs = $remain % 60;              // 用B整数分取余数 为剩下的秒数
    $res = array("day" => $days, "hour" => $hours, "min" => $mins, "sec" => $secs);
    return $res;
  }

</code>

The above introduces the PHP time function, which is ready to use, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

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