在做业务统计计算的时候难免会遇到时间节点问题,一般会涉及的也就是月份、起始日期,然后再数据库做统计查询的时候就会用到时间戳,简单写了下,把代码分享出来,给大家参考,当然有优化的意见,欢迎交流!
PHP:
/** * @param $date1 * @param $date2 * @return number * 计算两个日期相隔月数 */ function getMonthNum($date1,$date2){ $date1_stamp=strtotime($date1); $date2_stamp=strtotime($date2); list($date_1['y'],$date_1['m'])=explode("-",date('Y-m',$date1_stamp)); list($date_2['y'],$date_2['m'])=explode("-",date('Y-m',$date2_stamp)); return abs($date_1['y']-$date_2['y'])*12 +$date_2['m']-$date_1['m']; } /** * php计算两个日期之间相隔月数、每月开始日期、结束日期,开始时间戳,结束时间戳 */ function get_cal_date($start_date,$end_date){ $month=getMonthNum($start_date,$end_date); for ($m=0;$m<=$month;$m++){ $start_date=date('Y-m-01', strtotime($start_date)); $now_time=strtotime($start_date." +$m month"); $data[$m]['title']=date('Y-m', strtotime(date("Y-m-d",$now_time))); $begin_date=date('Y-m-01', strtotime(date("Y-m-d",$now_time))); $end_date=date('Y-m-d', strtotime("$begin_date +1 month -1 day")); $data[$m]['begin_date']=$begin_date; $data[$m]['end_date']=$end_date; $data[$m]['begin_time']=strtotime($begin_date.' 00:00:00'); $data[$m]['end_time']=strtotime($end_date.' 23:59:59'); } return $data; }
使用方法:
get_cal_date("2016-07-01","2017-04-08");
返回输出:
array(10) { [0] => array(5) { ["title"] => string(7) "2016-07" ["begin_date"] => string(10) "2016-07-01" ["end_date"] => string(10) "2016-07-31" ["begin_time"] => int(1467302400) ["end_time"] => int(1469980799) } [1] => array(5) { ["title"] => string(7) "2016-08" ["begin_date"] => string(10) "2016-08-01" ["end_date"] => string(10) "2016-08-31" ["begin_time"] => int(1469980800) ["end_time"] => int(1472659199) } [2] => array(5) { ["title"] => string(7) "2016-09" ["begin_date"] => string(10) "2016-09-01" ["end_date"] => string(10) "2016-09-30" ["begin_time"] => int(1472659200) ["end_time"] => int(1475251199) } [3] => array(5) { ["title"] => string(7) "2016-10" ["begin_date"] => string(10) "2016-10-01" ["end_date"] => string(10) "2016-10-31" ["begin_time"] => int(1475251200) ["end_time"] => int(1477929599) } [4] => array(5) { ["title"] => string(7) "2016-11" ["begin_date"] => string(10) "2016-11-01" ["end_date"] => string(10) "2016-11-30" ["begin_time"] => int(1477929600) ["end_time"] => int(1480521599) } [5] => array(5) { ["title"] => string(7) "2016-12" ["begin_date"] => string(10) "2016-12-01" ["end_date"] => string(10) "2016-12-31" ["begin_time"] => int(1480521600) ["end_time"] => int(1483199999) } [6] => array(5) { ["title"] => string(7) "2017-01" ["begin_date"] => string(10) "2017-01-01" ["end_date"] => string(10) "2017-01-31" ["begin_time"] => int(1483200000) ["end_time"] => int(1485878399) } [7] => array(5) { ["title"] => string(7) "2017-02" ["begin_date"] => string(10) "2017-02-01" ["end_date"] => string(10) "2017-02-28" ["begin_time"] => int(1485878400) ["end_time"] => int(1488297599) } [8] => array(5) { ["title"] => string(7) "2017-03" ["begin_date"] => string(10) "2017-03-01" ["end_date"] => string(10) "2017-03-31" ["begin_time"] => int(1488297600) ["end_time"] => int(1490975999) } [9] => array(5) { ["title"] => string(7) "2017-04" ["begin_date"] => string(10) "2017-04-01" ["end_date"] => string(10) "2017-04-30" ["begin_time"] => int(1490976000) ["end_time"] => int(1493567999) } }