php时间函数,即拿即用

  • 时间差函数

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;
}
  • 倒计时天数小时分秒函数

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;
  }

相关推荐