PHP:根据 天、月、年 数据,返回前 N 天、月、年 数据;根据 天、月、年 数组,返回开始、结束时间戳;获取任意 天、月、年 的开始结束时间戳

代码

  • 直接上代码
<?php

class Demo
{
    //开始函数
    public function test()
    {
        # 根据 天、月、年 数据,返回前 N 天、月、年 数据
        $result = self::getMoreDays('2021-06-01');
        var_export($result);//array ( 0 => '2021-05-26', 1 => '2021-05-27', 2 => '2021-05-28', 3 => '2021-05-29', 4 => '2021-05-30', 5 => '2021-05-31', 6 => '2021-06-01', )
        echo '<br/>';

        $result = self::getMoreMonths('2021-06');
        var_export($result);//array ( 0 => '2020-12', 1 => '2021-01', 2 => '2021-02', 3 => '2021-03', 4 => '2021-04', 5 => '2021-05', 6 => '2021-06', )
        echo '<br/>';

        $result = self::getMoreYears('2021');
        var_export($result);//array ( 0 => '2015', 1 => '2016', 2 => '2017', 3 => '2018', 4 => '2019', 5 => '2020', 6 => '2021', )
        echo '<br/>';

        # 根据 天、月、年 数组,返回开始、结束时间戳
        list($startUnix, $endUnix) = self::getDaysUnix(['2021-06-01', '2021-06-02', '2021-06-03']);
        echo $startUnix . ' ' . $endUnix . ' <br/>'; //1622476800 1622735999

        list($startUnix, $endUnix) = self::getMonthsUnix(['2021-06', '2021-07', '2021-08']);
        echo $startUnix . ' ' . $endUnix . ' <br/>'; //1622476800 1630425599


        list($startUnix, $endUnix) = self::getYearsUnix(['2021', '2023']);
        echo $startUnix . ' ' . $endUnix . ' <br/>'; //1609430400 1704038399

        # 获取任意 天、月、年 的开始结束时间戳
        list($startUnix, $endUnix) = self::getOneDayUnix('2021-06-01');
        echo $startUnix . ' ' . $endUnix . ' <br/>'; //1622476800 1622563199

        list($startUnix, $endUnix) = self::getOneMonthUnix('2021-06');
        echo $startUnix . ' ' . $endUnix . ' <br/>'; //1622476800 1625068799

        list($startUnix, $endUnix) = self::getOneYearUnix('2021');
        echo $startUnix . ' ' . $endUnix . ' <br/>'; //1609430400 1640966399
    }

    /**
     * 获取任意天的前N天所有数据
     * @param string $time 日期:天 如 2021-06-01
     * @param int $number 前N天,默认7
     * @return array 结果数据:如 array ( 0 => '2021-05-26', 1 => '2021-05-27', 2 => '2021-05-28', 3 => '2021-05-29', 4 => '2021-05-30', 5 => '2021-05-31', 6 => '2021-06-01', )
     */
    public function getMoreDays($time, $number = 7)
    {
        $result = [$time];
        for ($i = 1; $i < $number; $i++) {
            $beforeDay = date("Y-m-d", strtotime($time . '-01' . " -{$i} days")); //注意:可以使用复数或者单数(即加‘s’或不加‘s’)
            array_unshift($result, $beforeDay); //把数据追加到数组最前面
        }
        return $result;
    }

    /**
     * 获取任意月的前N月所有数据
     * @param string $time 日期:月 如 2021-06
     * @param int $number 前N月,默认7
     * @return array 结果数据:如 array ( 0 => '2020-12', 1 => '2021-01', 2 => '2021-02', 3 => '2021-03', 4 => '2021-04', 5 => '2021-05', 6 => '2021-06', )
     */
    public function getMoreMonths($time, $number = 7)
    {
        $result = [$time];
        for ($i = 1; $i < $number; $i++) {
            $beforeMonth = date("Y-m", strtotime($time . '-01' . " -{$i} months")); //注意:可以使用复数或者单数(即加‘s’或不加‘s’)
            array_unshift($result, $beforeMonth); //把数据追加到数组最前面
        }
        return $result;
    }

    /**
     * 获取任意年的前N年所有数据
     * @param string $time 日期:年 如 2021
     * @param int $number 前N年,默认7
     * @return array 结果数据:如 array ( 0 => '2015', 1 => '2016', 2 => '2017', 3 => '2018', 4 => '2019', 5 => '2020', 6 => '2021', )
     */
    public function getMoreYears($time, $number = 7)
    {
        $result = [$time];
        for ($i = 1; $i < $number; $i++) {
            $beforeMonth = date("Y", strtotime($time . '-01' . " -$i years")); //注意:可以使用复数或者单数(即加‘s’或不加‘s’)
            array_unshift($result, $beforeMonth); //把数据追加到数组最前面
        }
        return $result;
    }

    /**
     * 根据每天数组,返回每天的开始、结束时间戳
     * @param array $days ['2021-06-01', '2021-06-02', '2021-06-03']
     * @return array [1622476800, 1622735999 ]
     */
    public function getDaysUnix($days)
    {
        //开始时间
        $startTime = $days[0] . ' 00:00:00'; //1天的开始时间
        $startUnix = strtotime($startTime);

        //结束时间
        $endTime = end($days) . ' 23:59:59'; //1天的结束时间
        $endUnix = strtotime($endTime);

        return [$startUnix, $endUnix];
    }

    /**
     * 根据月份数组,返回月份的开始、结束时间戳
     * @param array $months ['2021-06', '2021-07', '2021-08']
     * @return array [1622476800, 1630425599]
     */
    public function getMonthsUnix($months)
    {
        # 开始时间戳
        $startTime = $months[0] . '-01 00:00:00'; //开始时间
        $startUnix = strtotime($startTime); //开始时间戳

        # 结束时间戳
        $endMonth = end($months); //获取最后一条数据
        $endMonthDay = date('t', strtotime($endMonth)); //获取月份的天数

        $endTime = $endMonth . "-{$endMonthDay} 23:59:59"; //结束时间
        $endUnix = strtotime($endTime); //结束时间戳

        return [$startUnix, $endUnix];
    }

    /**
     * 根据年份数组,返回年份的开始、结束时间戳
     * @param array $years ['2021', '2022', '2023']
     * @return array [1609430400, 1704038399]
     */
    public function getYearsUnix($years)
    {
        //开始时间
        $startTime = $years[0] . '-01-01 00:00:00'; //一年的第一天固定:直接拼接日期
        $startUnix = strtotime($startTime);
        //结束时间
        $endTime = end($years) . '-12-31 23:59:59'; //一年的最后一天固定:直接拼接日期
        $endUnix = strtotime($endTime);

        return [$startUnix, $endUnix];
    }

    /**
     * 获取任意天的开始结束时间戳
     * @param string $day 2021-06-01
     * @return array [1622476800, 1622563199 ]
     */
    public function getOneDayUnix($day)
    {
        $startTime = $day . ' 00:00:00'; //一天的开始时间
        $startUnix = strtotime($startTime); //一天的开始时间戳

        $endTime = $day . ' 23:59:59'; //一天的结束时间
        $endUnix = strtotime($endTime); //一天的结束时间戳

        return [$startUnix, $endUnix];
    }

    /**
     * 获取任意月的开始结束时间戳
     * @param string $month 2021-06
     * @return array [1622476800, 1625068799]
     */
    public function getOneMonthUnix($month)
    {
        $startTime = $month . '-01 00:00:00'; //月份的第一天
        $startUnix = strtotime($startTime); //月份的第一天时间戳

        $endMonthDay = date('t', strtotime($month)); //获取最后一个月的天数
        $endTime = $month . "-{$endMonthDay} 23:59:59"; //月份最后一天的结尾日期
        $endUnix = strtotime($endTime); //月份最后一天的时间戳

        return [$startUnix, $endUnix];
    }

    /**
     * 获取任意年的开始结束时间戳
     * @param string $year 2021
     * @return array [1609430400, 1640966399]
     */
    public function getOneYearUnix($year)
    {
        $startTime = $year . '-01-01 00:00:00'; //1年的开始时间
        $startUnix = strtotime($startTime); //1年的开始时间戳

        $endTime = $year . '-12-31 23:59:59'; //1年的结束时间
        $endUnix = strtotime($endTime); //1年的结束时间戳

        return [$startUnix, $endUnix];
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值