获取每天时间戳,每周时间戳,每月时间戳和每年时间戳

https://github.com/wangyang0210/TimeStamp/tree/master 

 

只是一个思路,后面还会不断的优化,如果您有更好的方法和思路请在评论中写出,我会及时更新并标注来源

封装的方法

 

php

/**
 *   得到对应的时间戳
 *   @method  getTime
 *   deep
 *   @param   [string]   $year   [年份]
 *   @param   [string]   $month  [月份]
 *   @return  [array]    ['YearDay','MonthDay','normalWeek','week','month']       [时间戳]
 */
function getTime($year,$month)
{
    $monthNumber = $month;
    $month = sprintf("%02d", intval($month));
    $year = str_pad(intval($year), 4, "0", STR_PAD_RIGHT);
    $month>12 || $month<1 ? $month=1 : $month=$month;
    $start = strtotime($year . $month . "01000000");
    $startstr = date("Y-m-01", $start);
    $end = strtotime(date('Y-m-d 23:59:59', strtotime("$startstr +6 day")));
    $Monthend = strtotime(date('Y-m-d 23:59:59', strtotime("$startstr +1 month -1 day")));
    $days = date('t',$start);

    //天(每年中每天的开始和结束)
    $time = mktime(21,21,21,2,1,$year);
    $yearDays = date('t',$time)==29?'366':'365';
    $YearDay['1']['start'] = strtotime($year . '-01-01 00:00:00');
    $YearDay['1']['end'] = $YearDay['1']['start']+86399;
    for ($i=2; $i <=$yearDays ; $i++) {
        $YearDay[$i]['start'] = $YearDay[$i - 1]['end'] + 1;
        $YearDay[$i]['end'] = $YearDay[$i - 1]['end'] + 86400;
    }

    //天(每月每天的开始和结束)
    $MonthDay['1']['start'] = $start;
    $MonthDay['1']['end'] = $MonthDay['1']['start']+86399;
    for ($i=2; $i <= $days ; $i++) {
        $MonthDay[$i]['start'] = $MonthDay[$i - 1]['end'] + 1;
        $MonthDay[$i]['end'] = $MonthDay[$i - 1]['end'] + 86400;
    }

    //天(倒退30天的开始和结束)
    $backDays = [];
    for($i=0;$i<=30;$i++){
        $backDays[$i]['start']=strtotime(date("Y-m-d 00:00:00",strtotime('-'.$i.'day')));
        $backDays[$i]['end']=strtotime(date("Y-m-d 23:59:59",strtotime('-'.$i.'day')));
    }
    asort($backDays);
    //周(将一个月以每七天为一周划分的开始和结束)
    $num = ceil($days/7);
    $weekSeven['1']['start'] = $start;
    $weekSeven['1']['end'] = $end;
    for ($i=2; $i <= $num ; $i++) {
        $weekSeven[$i]['start'] = $weekSeven[$i - 1]['end'] + 1;
        $weekSeven[$i]['end'] = $weekSeven[$i - 1]['end'] + 604800;
    }
    $weekSeven[count($weekSeven)]['end'] = $Monthend;

    //周(一个月按正常的每周划分)
    $time = $year . '-' . $month;
    $weekday = date('w', strtotime($time));
    if ($weekday == 1) {      //1号是周一
        $firstWeekDay = 7;
    } elseif ($weekday == 0) {      //1号是周日
        $firstWeekDay = 1;
    } else {
        $firstWeekDay = 8 - $weekday;
    }
    $endThisweek = mktime(23, 59, 59, $month, $firstWeekDay, $year);
    $weekArray[1] = [strtotime($time), $endThisweek];
    $weeknum = 1 + ceil((date('t', $month) - $firstWeekDay) / 7);   //获取到本月一共有几周

    $beginThisWeek = mktime(0, 0, 0, $month, $firstWeekDay + 1, $year);
    $endThisweek = mktime(23, 59, 59, $month, $firstWeekDay + 7, $year);
    for ($i = 2; $i <= $weeknum; $i++) {
        $weekArray[$i] = [($beginThisWeek + ($i - 2) * 7 * 86400), ($endThisweek + ($i - 2) * 7 * 86400)];
    }
    //获取每周的时间节点
    $weekArray[$weeknum] = [$weekArray[$weeknum][0], mktime(23, 59, 59, $month, date('t', strtotime($time)), $year)];

    //周(每年的正常周)
    $yearFirstDay = $year.'-01-01 00:00:00';
    $yearEndDay   = $year.'-12-31 23:59:59';
    $week = date('N',strtotime($yearFirstDay));
    //当年第一周的开始时间和结束时间(开始时间不一定是星期一)
    $days = 8-$week;
    $firstWeekendDay = date('Y-m-d H:i:s',strtotime($year.'-01-0'.$days.' 23:59:59'));
    $weeks = [];
    $weeks[1]['start'] = strtotime($yearFirstDay);
    $weeks[1]['end']   = strtotime($year.'-01-0'.$days.' 23:59:59');
    //组装一年中 完整的各周开始和结束时间戳
    $days2 = ((strtotime($yearEndDay)-$weeks[1]['end'])/86400)/7;
    $days3 = (int)floor($days2);
    $length = $days3+1;
    for($i=2;$i<=$length;$i++) {
        $weeks[$i]['start'] = 1+$weeks[$i-1]['end'];
        $weeks[$i]['end']   = 604800+$weeks[$i-1]['end'];
    }
    //组装最后一周的开始和结束时间
    $remainder = ((strtotime($yearEndDay)-$weeks[1]['end'])/86400)%7;
    if($remainder > 0) {
        $data = array();
        $data['start'] = 1+end($weeks)['end'];
        $data['end']   = ($remainder*86400)+end($weeks)['end'];
        array_push($weeks,$data);
    }
    //每月(每年中每月的开始和结束)
    $month = [];
    for ($i = 1; $i <= 12; $i++) {
        $mon = sprintf("%02d", intval($i));
        $mon > 12 || $mon < 1 ? $mon = 1 : $mon = $mon;
        $mStart = strtotime($year . $mon . "01000000");
        $mStartCode = date("Y-m-01", $mStart);
        $mEnd = strtotime(date('Y-m-d 23:59:59', strtotime("$mStartCode +1 month -1 day")));
        $month[$i]['start'] = $mStart;
        $month[$i]['end'] = $mEnd;
    }
    return ['yearDay'=>$YearDay,'monthDay'=>$MonthDay,'backDay'=>$backDays,'normalWeek'=>$weekArray,'weekSeven'=>$weekSeven,'yearWeek'=>$weeks,'month'=>$month];

}

 

 

 

获取时间范围内的时间戳

  七天一周/30天一个月

php

/*
* @Author: wyy
* @Date:   2018-12-16 20:32:47
* @Email:  [email protected]
* @Last Modified by:   wyy
* @Last Modified time: 2018-12-16 21:38:48
 */


/**
 *   得到对应的时间戳
 *   @method  getRangTime
 *   deep
 *   @param   [int]   $start  [开始时间戳]
 *   @param   [int]   $end    [结束时间戳]
 *   @param   array    $data     [时间戳]
 *   @param   string   $mark    [标识符]
 *   @return  [array]  $data    [指定的时间戳]
 */
 function getRangTime($start,$end,$data=[],$mark='d'){
        $data[] = $start;
        if($start<$end){
            if ($mark == 'd') {
                $data = getRangTime($start+60*60*24,$end,$data,$mark);
            } elseif ($mark == 'w') {
                $data = getRangTime($start+60*60*24*7,$end,$data,$mark);
            } elseif ($mark == 'm') {
                $data = getRangTime($start+60*60*24*30,$end,$data,$mark);
            } elseif ($mark == 'y') {
                $data = getRangTime($start+60*60*24*365,$end,$data,$mark);
            }
        }
        return $data;
    }

 

 

 

 

单独的方法

/**
 *   每年的每天
 *   @method  getYearDay
 *   deep
 *   @param   [string]      $year  [年]
 *   @return  [array]       $day   [每年的每天的开始和结束]
 */
function getYearDay($year){

    //判断是否为闰年
    $time = mktime(21,21,21,2,1,$year);
    $yearDays = date('t',$time)==29?'366':'365';
    $day = [];
    $day['1']['start'] = strtotime($year . '-01-01 00:00:00');
    $day['1']['end'] = $day['1']['start']+86399;
    for ($i=2; $i <=$yearDays ; $i++) {
        $day[$i]['start'] = $day[$i - 1]['end'] + 1;
        $day[$i]['end'] = $day[$i - 1]['end'] + 86400;
    }

    return $day;

}

/**
 *   获取一周中每天的开始和结束时间
 * @method  getDay
 *
 *
 * @param   [string]  $year     [年份]
 * @param   [string]  $weekday  [周]
 *
 * @return  [array]   $data     [该周内每天的开始和结束时间]
 */
function getDay($year, $weekday)
{
    //一年时间
    $yearFirstDay = $year . '-01-01 00:00:00';
    $yearEndDay = $year . '-12-31 23:59:59';

    //第一天是星期几
    $week = date('N', strtotime($yearFirstDay));

    $days = 8 - $week;
    $firstWeekendDay = date('Y-m-d H:i:s', strtotime($year . '-01-0' . $days . ' 23:59:59'));
    $weeks = array();
    $weeks[1]['week_start'] = strtotime($yearFirstDay);
    $weeks[1]['week_end'] = strtotime($year . '-01-0' . $days . ' 23:59:59');
    //各周开始和结束时间戳
    $days2 = ((strtotime($yearEndDay) - $weeks[1]['week_end']) / 86400) / 7;
    $days3 = (int)floor($days2);
    $length = $days3 + 1;
    for ($i = 2; $i <= $length; $i++) {
        $weeks[$i]['week_start'] = 1 + $weeks[$i - 1]['week_end'];
        $weeks[$i]['week_end'] = 604800 + $weeks[$i - 1]['week_end'];
    }
    //最后一周的开始和结束时间
    $remainder = ((strtotime($yearEndDay) - $weeks[1]['week_end']) / 86400) % 7;
    if ($remainder > 0) {
        $data = [];
        $data['week_start'] = 1 + end($weeks)['week_end'];
        $data['week_end'] = ($remainder * 86400) + end($weeks)['week_end'];
        array_push($weeks, $data);
    }
    //获取每天的开始和结束时间
    if (array_key_exists($weekday, $weeks)) {

        $wEnd = $weeks[$weekday]['week_end'];

        $data = [];
        $data[1]['start'] = $weeks[$weekday]['week_start'];
        $data[1]['end'] = $data[1]['start'] + 86399;
        for ($i = 2; $i < 8; $i++) {
            $data[$i]['start'] = $data[$i - 1]['end'] + 1;
            $data[$i]['end'] = $data[$i - 1]['end'] + 86400;
        }
        return $data;
    };
}


/**
 *   动态获取某月的周的时间
 * @method  getWeekCount
 *
 *
 * @param   [string]        $year   [年]
 * @param   [string]        $month  [月]
 *
 * @return  [array]         $weekArray [每周开始和结束周时间戳]
 */
function getWeekCount($year, $month)
{
    $time = $year . '-' . $month;
    $weekday = date('w', strtotime($time));      //获取1号是周几
    if ($weekday == 1) {      //1号是周一
        $firstWeekDay = 7;
    } elseif ($weekday == 0) {      //1号是周日
        $firstWeekDay = 1;
    } else {
        $firstWeekDay = 8 - $weekday;
    }
    $weekArray = [];
    $endThisweek = mktime(23, 59, 59, $month, $firstWeekDay, $year);
    $weekArray[1] = [strtotime($time), $endThisweek];
    $weeknum = 1 + ceil((date('t', $month) - $firstWeekDay) / 7);   //获取到本月一共有几周

    $beginThisWeek = mktime(0, 0, 0, $month, $firstWeekDay + 1, $year);
    $endThisweek = mktime(23, 59, 59, $month, $firstWeekDay + 7, $year);
    for ($i = 2; $i <= $weeknum; $i++) {
        $weekArray[$i] = [($beginThisWeek + ($i - 2) * 7 * 86400), ($endThisweek + ($i - 2) * 7 * 86400)];
    }

    //获取每周的时间节点
    $weekArray[$weeknum] = [$weekArray[$weeknum][0], mktime(23, 59, 59, $month, date('t', strtotime($time)), $year)];

    if ($year == date("Y") && $month == date("m")) {
        $nownum = ceil((date('d') - $firstWeekDay) / 7) + 1;  //获取当前是第几周,如果不是当月默认为第一周
    } else {
        $nownum = 1;
    }
    return [$weekArray,$nownum];
}


/**
 *   动态获取每月的开始和结束时间戳
 * @method  getMonth
 *
 *
 * @param   [string]    $year      [年份]
 *
 * @return  [array]     $data     [每月的开始和结束]
 */
function getMonth($year)
{
    $year = str_pad(intval($year), 4, "0", STR_PAD_RIGHT);
    $data = [];
    for ($i = 1; $i <= 12; $i++) {
        $month = sprintf("%02d", intval($i));
        $month > 12 || $month < 1 ? $month = 1 : $month = $month;
        $mStart = strtotime($year . $month . "01000000");
        $mStartCode = date("Y-m-01", $mStart);
        $mEnd = strtotime(date('Y-m-d 23:59:59', strtotime("$mStartCode +1 month -1 day")));
        $data[$i]['mstart'] = $mStart;
        $data[$i]['mend'] = $mEnd;
    }
    return $data;
}

/**
 *   每天的开始和结束
 *   @method  getMonthDay
 *   deep
 *   @param   [string]       $year   [年]
 *   @param   [string]       $month  [月]
 *   @return  [array]        $data     [每天的开始和结束]
 */
function getMonthDay($year, $month){
    $month = sprintf("%02d", intval($month));
    $year = str_pad(intval($year), 4, "0", STR_PAD_RIGHT);
    $month>12 || $month<1 ? $month=1 : $month=$month;
    $start = strtotime($year . $month . "01000000");
    $startstr = date("Y-m-01", $start);
    $end = strtotime(date('Y-m-d 23:59:59', strtotime("$startstr +1 month -1 day")));
    $days = date('t',$start);
    $data['1']['start'] = $start;
    $data['1']['end'] = $data['1']['start']+86399;
    for ($i=2; $i <= $days ; $i++) {
        $data[$i]['start'] = $data[$i - 1]['end'] + 1;
        $data[$i]['end'] = $data[$i - 1]['end'] + 86400;
    }
    return $data;
}


/**
 *   每月的每周的开始和结束(以七天为标准)
 *   @method  getMonthWeek
 *   deep
 *   @param   [string]       $year   [年]
 *   @param   [string]       $month  [月]
 *   @return  [array]        $data   [每周的开始和结束]
 */
function getMonthWeek($year, $month){
    $month = sprintf("%02d", intval($month));
    $year = str_pad(intval($year), 4, "0", STR_PAD_RIGHT);
    $month>12 || $month<1 ? $month=1 : $month=$month;
    $start = strtotime($year . $month . "01000000");
    $startstr = date("Y-m-01", $start);
    $end = strtotime(date('Y-m-d 23:59:59', strtotime("$startstr +6 day")));
    $Monthend = strtotime(date('Y-m-d 23:59:59', strtotime("$startstr +1 month -1 day")));
    $days = date('t',$start);
    $num = ceil($days/7);
    $data['1']['start'] = $start;
    $data['1']['end'] = $end;
    for ($i=2; $i <= $num ; $i++) {
        $data[$i]['start'] = $data[$i - 1]['end'] + 1;
        $data[$i]['end'] = $data[$i - 1]['end'] + 604800;
    }
    $data[count($data)]['end'] = $Monthend;
    return $data;

}

 

/**
* 得到一年的所有周
* @method get_week
* deep
* @param [string] $year [年]
* @return [array] $data [一年的所有周起始]
*/
function get_week($year) {
//获取当年第一天的日期
$yearFirstDay = $year.'-01-01 00:00:00';
$yearEndDay = $year.'-12-31 23:59:59';

//查看第一天是星期几
$week = date('N',strtotime($yearFirstDay));
//当年第一周的开始时间和结束时间(开始时间不一定是星期一)
$days = 8-$week;
$firstWeekendDay = date('Y-m-d H:i:s',strtotime($year.'-01-0'.$days.' 23:59:59'));
$weeks = array();
$weeks[1]['week_start'] = strtotime($yearFirstDay);
$weeks[1]['week_end'] = strtotime($year.'-01-0'.$days.' 23:59:59');
//组装一年中 完整的各周开始和结束时间戳
$days2 = ((strtotime($yearEndDay)-$weeks[1]['week_end'])/86400)/7;
$days3 = (int)floor($days2);
$length = $days3+1;
for($i=2;$i<=$length;$i++) {
$weeks[$i]['week_start'] = 1+$weeks[$i-1]['week_end'];
$weeks[$i]['week_end'] = 604800+$weeks[$i-1]['week_end'];
}
//组装最后一周的开始和结束时间
$remainder = ((strtotime($yearEndDay)-$weeks[1]['week_end'])/86400)%7;
if($remainder > 0) {
$data = array();
$data['week_start'] = 1+end($weeks)['week_end'];
$data['week_end'] = ($remainder*86400)+end($weeks)['week_end'];
array_push($weeks,$data);
}

return $weeks;
}

 

转载于:https://www.cnblogs.com/wangyang0210/p/10101985.html

你可能感兴趣的:(javascript,数据库,php,ViewUI)