时间日期函数库


/**
 * 转换为时间戳
 * @param type $source 日期
 * @param string $condition
 * @param type $now
 * @return type
 */
function to_timestamp($source, $condition = "", $now = null)
{
    if (is_numeric($source) || empty($source))
    {
        return $source;
    }
    //添加空格
    if (!empty($condition))
    {
        $condition = " " . $condition;
    }
    if (is_array($source))
    {
        if (!empty($source[1]) && is_array($source[1]))
        {
            //between 格式
            $resoruce = $source;
            $source = $source[1];
        }
        $source = to_date($source);
        foreach ($source as $key => $value)
        {
            if (!empty($now))
            {
                $source[$key] = strtotime($value . "$condition", $now);
            }
            else
            {
                $source[$key] = strtotime($value . "$condition");
            }
        }
        if (!empty($resoruce[1]) && is_array($resoruce[1]))
        {
            //between 格式
            $resoruce[1] = $source;
            $source = $resoruce;
        }
    }
    else
    {
        $source = to_date($source);
        if (!empty($now))
        {
            $source = strtotime($source . "$condition", $now);
        }
        else
        {
            $source = strtotime($source . "$condition");
        }
        return $source;
    }
    return $source;
}
/*
 * 多字节的字符串替换
 */
function mb_strtr($source, array $contrast,$encoding="utf-8")
{
    mb_regex_encoding($encoding);
    foreach ($contrast as $key => $value)
    {
        $source = mb_ereg_replace($key, $value, $source);
    }
    return $source;
}
/**
 * 日期(时间戳)比较,$date1>$date2 返回true
 * @param type $date1
 * @param type $date2
 * @return type
 */
function date_compare($date1, $date2)
{
    $date1 = to_date($date1);
    $date2 = to_date($date2);
    $time1 = to_timestamp($date1);
    $time2 = to_timestamp($date2);
    return $time1 > $time2;
}

/**
 * 转换为日期
 * @param type $source 时间戳或者日期,必须完整,如年月日,年月日时分秒(数组可以批量转换)
 * @param string $format ,格式字符串
 * @return mixed
 */
function to_date($source, $format = "Y-m-d H:i:s")
{
    $temp = $source;
    if (empty($source))
    {
        return $temp;
    }
    if (is_array($source))
    {
        if (!empty($source[1]) && is_array($source[1]))
        {
            //between 格式
            $resoruce = $source;
            $source = $source[1];
        }
        foreach ($source as $key => $value)
        {
            $source[$key] = to_date($value, $format);
        }
        if (!empty($resoruce[1]) && is_array($resoruce[1]))
        {
            //between 格式
            $resoruce[1] = $source;
            $source = $resoruce;
        }
    }
    else
    {   if (is_numeric($source)&&$source<=9999)
        {
            $source .= '年'; 
        }
        if (!is_numeric($source))
        {
            $contrast = [
                "年|月" => '-',
                "日|(日\s)" => ' ',
                "时|分" => ':',
                "秒" => '',
                "\-$" => '-1',
                ":$" => ':0',
            ];
            $source = mb_strtr($source, $contrast);
            $source = strtotime($source) ? strtotime($source) : null;
        }
        return date($format, $source);
    }
    return $source;
}

/*
 * 获取某天的开头结束
 */

function get_the_day($date, $type = "date") {
    if ($type == "date") {
        $firstday = date('Y-m-d' . ' 00:00:00', strtotime($date));
        $lastday = date('Y-m-d' . ' 23:59:59', strtotime($date));
    } else {
        $firstday = date('Y-m-d' . ' 00:00:00', strtotime($date));
        $lastday = date('Y-m-d' . ' 23:59:59', strtotime($date));
        $firstday = strtotime($firstday);
        $lastday = strtotime($lastday);
    }

    return array($firstday, $lastday);
}

/*
 * 获取某月开头结尾
 */

function get_the_month($date, $his = "00:00:00", $type = "date", $today_flag = true) {
    $lastday = $firstday = date('Y-m-01 ' . $his, strtotime($date));
    $next_month_final_day = strtotime("$firstday +1 month -1 second");
    $today = time();
    if ($today < $next_month_final_day && $today_flag&&strtotime($date)<$today) {
        $lastday = date('Y-m-d ' . "23:59:59", $today);
    } else {
        $lastday = date('Y-m-d ' . "23:59:59", $next_month_final_day);
    }
    if ($type != "date") {
        $firstday = strtotime($firstday);
        $lastday = strtotime($lastday);
    }
    return array($firstday, $lastday);
}

/*
 * 获取季度的时间限制条件(当前季度加上往前$num个季度)
 */
function quarter_condition_before($date,$num=4)
{
    $quarter_condition = [];
    for ($index = 0; $index < $num; $index++)
    {
        if ($index ==0)
        {
            $temp = quarter_condition(date("Y"), $date);
        }
        else
        {
            $temp = quarter_condition(date("Y"), $date,1);
        }
        $quarter_condition = array_merge($temp,$quarter_condition);
        $date = array_column_one($temp)[1][0];
    }
    return $quarter_condition;
}

/*
 * 获取季度的时间限制条件
 * @param $year 年
 * @param $date 获取某个日期的具体季度,会用该项的年份替代$year
 * @param $last 上一年
 *  @return array 
 */
function quarter_condition($year,$date=FALSE,$last=0)
{
    if (!empty($date))
    {
        $year = to_date($date,"Y");
    }
    $quarter = utilsStatus('quarter');
    $new_quarter = [];
    foreach ($quarter as $key => $value)
    {
        $new_quarter[$year." ".$key] = $value;
        foreach ($value[1] as $key2 => $value2)
        {
            $new_quarter[$year." ".$key][1][$key2] =to_date($year . "-" . $value2);
        }
    }
    $quarter = $new_quarter;
    if (!empty($date))
    {
        $date = to_date($date);
        $date = get_the_month($date)[0];
        $date_timestamp = to_timestamp($date);
        foreach ($quarter as $quarter_name => $value)
        {
            if (to_timestamp($value[1][0])<=$date_timestamp&&$date_timestamp<=to_timestamp($value[1][1]))
            {
                $quarter =[$quarter_name=>$quarter[$quarter_name]];
                break;
            }
        }
    }
    if (!empty($last))
    {
        $last_timestamp = to_timestamp($date, "-3month");
        $last_date = to_date($last_timestamp);
        return quarter_condition($year,$last_date);
    }
    return $quarter;
}

    /**
     * 获取两个日期之间的周日期
     * @param type $date_arr
     * @param type $num
     * @return arr 二维数组,一维编号,二维日期
     */
    public function week_list($date_arr,$num = 5)
    {
        $end_date = $date_arr[1];
        $date = $date_arr[0];
        $start_date = $date_arr[0];
        $start_timestamp = to_timestamp($date);
        $timestamp = to_timestamp($date);
        $end_timestamp = to_timestamp($end_date);
        $week_list = [];
        $i = 0;
        while ($timestamp <= $end_timestamp)
        {
            $week_day = to_date($timestamp, 'w');
//            0是星期天
            if ($week_day == 0)
            {
                $week_start_date = to_date($timestamp - 6 * 3600 * 24, "Y-m-d 00:00:00");
                if (date_compare($start_date, $week_start_date))
                {
                    $week_start_date = $start_date;
                }
                $week_end_date = to_date($timestamp, "Y-m-d 23:59:59");
                $week_list[$i] = [
                    $week_start_date,
                    $week_end_date,
                ];
                $i++;
            }
            if (date_compare($timestamp + 3600 * 24, $end_date))
            {
                if (!empty($week_list[$i - 1]))
                {
                    $week_start_timestamp = to_timestamp($week_list[$i - 1][1]) + 1;
                    $week_start_date = to_date($week_start_timestamp);
                }
                else
                {
                    $week_start_date = to_date($timestamp, "Y-m-d 00:00:00");
                }
                $week_end_date = to_date($timestamp, "Y-m-d 23:59:59");
                $week_list[$i] = [
                    $week_start_date,
                    $week_end_date,
                ];
            }
            $timestamp += 3600 * 24;
        }
        $return = [];
        for ($i = 0; $i < $num; $i++)
        {
            if (!empty($week_list[$i]))
            {
                $return[] = $week_list[$i];
            }
        }
        return $return;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值