返回 指定时间戳 的 指定表达式的 开始和结束 时间戳
in PHP with 0 comment

返回 指定时间戳 的 指定表达式的 开始和结束 时间戳

in PHP with 0 comment

返回 指定时间戳 的 指定表达式的 开始和结束 时间戳

应用场景

为简单明了的解决以上问题,所以有了该函数。

功能

函数返回 给定时间戳 or 默认当前时间戳表达式(today,this_month ...)开始 和 结束 时间戳。

参数

by

unix_time
可选参数,指定时间。

函数

    /**
     * 函数返回指定日期中 (begin, end) 的时间戳组成的数组
     * 结果可为:指定时间戳计算以下内容时间戳:
     * 今天(today),昨天(yesterday),明天(tomorrow),本月(this_month),上月(last_month),
     * 下月(next_month),今年(this_year),去年(last_year),明年(next_year),
     * 本周(this_week),上周(last_week), 下周(next_week)
     * @param string $by
     * @param integer $unix_time
     * @return array
     * @author Tongz <root@rooot.me>
     */
    function getUnixTimeByCondition ($by = 'today', $unix_time = 0) {
        $data = [
            'begin' => 0,
            'end'   => 0
        ];

        $time = empty($unix_time) ? time() : $unix_time;

        switch ($by) {

            case 'yesterday' :
                $data['begin'] = mktime(0, 0, 0, date('m', $time), date('d', $time) - 1, date('Y', $time));
                $data['end'] = mktime(0, 0, 0, date('m', $time), date('d', $time), date('Y', $time)) - 1;
            break;

            case 'tomorrow' :
                $data['begin'] = mktime(0, 0, 0, date('m', $time), date('d', $time) + 1, date('Y', $time));
                $data['end'] = mktime(0, 0, 0, date('m', $time), date('d', $time) + 2, date('Y', $time)) - 1;
            break;

            case 'this_month' :
                $data['begin'] = mktime(0, 0, 0, date('m', $time), 1, date('Y', $time));
                $data['end'] = mktime(0, 0, 0, date('m', $time) + 1, 1, date('Y', $time)) - 1;
            break;

            case 'last_month' :
                $data['begin'] = mktime(0, 0, 0, date('m', $time) - 1, 1, date('Y', $time));
                $data['end'] = mktime(0, 0, 0, date('m', $time), 1, date('Y', time())) - 1;
            break;

            case 'next_month' :
                $data['begin'] = mktime(0, 0, 0, date('m', $time) + 1, 1, date('Y', $time));
                $data['end'] = mktime(0, 0, 0, date('m', $time) + 2, 1, date('Y', $time)) - 1;
            break;

            case 'this_year' :
                $data['begin'] = mktime(0, 0, 0, 1, 1, date('Y', $time));
                $data['end'] = mktime(0, 0, 0, 1, 1, date('Y', $time) + 1) - 1;
            break;

            case 'last_year' :
                $data['begin'] = mktime(0, 0, 0, 1, 1, date('Y', $time) - 1);
                $data['end'] = mktime(0, 0, 0, 1, 1, date('Y', $time)) - 1;
            break;

            case 'next_year' :
                $data['begin'] = mktime(0, 0, 0, 1, 1, date('Y', $time) + 1);
                $data['end'] = mktime(0, 0, 0, 1, 1, date('Y', $time) + 2) - 1;
            break;

            case 'this_week' :
                $day = date('d', $time) - (date('N', $time) - 1);
                $data['begin'] = mktime(0, 0, 0, date('m', $time), $day, date('Y', $time));
                $data['end'] = mktime(0, 0, 0, date('m', $time), $day + 7, date('Y', $time)) - 1;
            break;

            case 'last_week' :
                $day = date('d', $time) - (date('N', $time) - 1) - 7;
                $data['begin'] = mktime(0, 0, 0, date('m', $time), $day, date('Y', $time));
                $data['end'] = mktime(0, 0, 0, date('m', $time), $day + 7, date('Y', $time)) - 1;
            break;

            case 'next_week' :
                $day = date('d', $time) - (date('N', $time) - 1) + 7;
                $data['begin'] = mktime(0, 0, 0, date('m', $time), $day, date('Y', $time));
                $data['end'] = mktime(0, 0, 0, date('m', $time), $day + 7, date('Y', $time)) - 1;
            break;

            default :
                $data['begin'] = mktime(0, 0, 0, date('m', $time), date('d', $time), date('Y', $time));
                $data['end'] = mktime(0, 0, 0, date('m', $time), date('d', $time) + 1, date('Y', $time)) - 1;
        }

        return $data;
    }

// 返回今天(2018-5-28 13:37)的开始和结束时间戳
$data = getUnixTimeByCondition('today');
/* 
返回值: 
array:2 [
  "begin" => 1527465600  // "2018-05-28 00:00:00"
  "end" => 1527551999    // "2018-05-28 23:59:59"
] 
*/

// 返回(2018-05-27 06:23:44)的 前一天的 开始和结束时间戳
$data = getUnixTimeByCondition('yesterday', 1527373424);
/* 
返回值: 
array:4 [
  "begin" => 1527206400 // "2018-05-25 00:00:00"
  "end" => 1527292799 // "2018-05-25 23:59:59"
]
*/

// 返回(2018-05-27 06:23:44)的 上个月 的 开始和结束时间戳
$date = getUnixTimeByCondition('last_month', 1527373424);
/* 
返回值:
array:4 [
  "begin" => 1522540800 // "2018-04-01 00:00:00"
  "end" => 1525132799 // "2018-04-30 23:59:59"
]
*/
Responses
icon_mrgreen.gificon_neutral.gificon_twisted.gificon_arrow.gificon_eek.gificon_smile.gificon_confused.gificon_cool.gificon_evil.gificon_biggrin.gificon_idea.gificon_redface.gificon_razz.gificon_rolleyes.gificon_wink.gificon_cry.gificon_surprised.gificon_lol.gificon_mad.gificon_sad.gificon_exclaim.gificon_question.gif