PHP时间戳格式化与日期处理函数

/**
 * @method  将unix时间戳转成字符
 * @param   int $time 默认当前时间
 * @param   string $format 默认格式Y-m-d H:i:s
 * @return  string
 */
function timeToStr($time = NULL, $format = NULL)
{
    if (!$time) {
        $time = time();
    }
    if (!$format) {
        $format = 'Y-m-d H:i:s';
    }
    return date($format, $time);
}
/**
 * @method  普通时间转Unix时间戳
 * @param   $dtime 普通时间格式(2019年03月16日15时51分55秒 / 2019-3-16 15:51:55)
 * @return  int
 */
function GetMkTime($dtime)
{
    if (!preg_match("/[^0-9]/", $dtime)) {
        return $dtime;
    }
    $dtime = trim($dtime);
    $dt = Array(1970, 1, 1, 0, 0, 0);
    $dtime = preg_replace("/[\r\n\t]|日|秒/", " ", $dtime);
    $dtime = str_replace("年", "-", $dtime);
    $dtime = str_replace("月", "-", $dtime);
    $dtime = str_replace("时", ":", $dtime);
    $dtime = str_replace("分", ":", $dtime);
    $dtime = trim(preg_replace("/[ ]{1,}/", " ", $dtime));
    $ds = explode(" ", $dtime);
    $ymd = explode("-", $ds[0]);
    if (!isset($ymd[1])) {
        $ymd = explode(".", $ds[0]);
    }
    if (isset($ymd[0])) {
        $dt[0] = $ymd[0];
    }
    if (isset($ymd[1])) $dt[1] = $ymd[1];
    if (isset($ymd[2])) $dt[2] = $ymd[2];
    if (strlen($dt[0]) == 2) $dt[0] = '20' . $dt[0];
    if (isset($ds[1])) {
        $hms = explode(":", $ds[1]);
        if (isset($hms[0])) $dt[3] = $hms[0];
        if (isset($hms[1])) $dt[4] = $hms[1];
        if (isset($hms[2])) $dt[5] = $hms[2];
    }
    foreach ($dt as $k => $v) {
        $v = preg_replace("/^0{1,}/", '', trim($v));
        if ($v == '') {
            $dt[$k] = 0;
        }
    }
    $mt = mktime($dt[3], $dt[4], $dt[5], $dt[1], $dt[2], $dt[0]);
    if (!empty($mt)) {
        return $mt;
    } else {
        return time();
    }
}
/**
 * @method 计算后7天时间并格式化
 * @param int $days
 * @return array
 *
 * Array
 * (
 * [1552724226] => 2019-03-16 周六 今天
 * [1552810626] => 2019-03-17 周日 明天
 * [1552897026] => 2019-03-18 周一 后天
 * [1552983426] => 2019-03-19 周二
 * )
 */
function getDays($days = 4)
{
    $dates = array();
    $str = array(' 今天', ' 明天', ' 后天');
    for ($i = 0; $i < $days; $i++) {
        $time = strtotime("+{$i} day");
        $dates[$time] = date('Y-m-d', $time) . " 周" . $this->getChineseWeek($time) . (isset($str[$i]) ? $str[$i] : '');
    }
    return $dates;
}
/**
 * @param int $days
 * @return array
 *
 * Array
 * (
 * [1] => Array
 * (
 * [timestamp] => 1552724429
 * [ydate] => 2019-03-16
 * [date] => 03-16
 * [week] => 周六
 * [str] =>  今天
 * )
 * )
 */
function getDays2($days = 7)
{
    $dates = array();
    $str = array(' 今天', ' 明天', ' 后天');
    for ($i = 0; $i < $days; $i++) {
        $time = strtotime("+{$i} day");
        $row = array(
            "timestamp" => $time,
            "ydate" => $this->timeToStr($time, 'Y-m-d'),
            "date" => $this->timeToStr($time, 'm-d'),
            "week" => "周" . $this->getChineseWeek($time),
            "str" => isset($str[$i]) ? $str[$i] : '',
        );
        $dates[$i + 1] = $row;
    }
    return $dates;
}
/**
 * @method 计算周几
 * @param $time Unix时间戳
 * @return string
 *
 */
function getChineseWeek($time)
{
    $weeks = array("日", "一", "二", "三", "四", "五", "六");
    $week = date("w", $time);
    return $weeks[$week];
}
/**
 * @method  计算发布时间与当前时间相隔几分钟、几小时前、几天前
 * @param   $time Unix时间戳
 * @return  bool|string
 */
function formatTime($time)
{
    $rtime = date("Y-m-d 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 . '小时前 ';
    } elseif ($time < 60 * 60 * 24 * 3) {
        $d = floor($time / (60 * 60 * 24));
        if ($d == 1) {
            $str = '昨天 ' . $rtime;
        } else {
            $str = '前天 ' . $rtime;
        }
    } elseif ($time < 60 * 60 * 24 * 7) {
        $str = '一周内' . $rtime;
    } else {
        $str = $rtime;
    }
    return $str;
}
/**
 * 计算上一个月的今天,如果上个月没有今天,则返回上一个月的最后一天
 * @param type $time
 * @return type
 */
function last_month_today($time){
    $last_month_time = mktime(date("G", $time), date("i", $time),
        date("s", $time), date("n", $time), 0, date("Y", $time));

    $last_month_t =  date("t", $last_month_time);

    if ($last_month_t < date("j", $time)) {
        return date("Y-m-t H:i:s", $last_month_time);
    }

    return date(date("Y-m", $last_month_time) . "-d", $time);
}
Last modification:December 3rd, 2019 at 05:49 pm
如果觉得我的文章对你有用,请随意赞赏

Leave a Comment