PHP时间处理汇总

PHP时间处理汇总

1.求上个月的开始时间和结束时间

1 <?php
2 header('Content-Type: text/html; charset=utf-8');
3 // 上个月的开始时间
4 $begin_time = date('Y-m-01',strtotime('-1 month'));
5 var_dump($begin_time);
6 // 上个月的结束时间
7 $end_time = date("Y-m-d", strtotime(-date('d').'day'));
8 var_dump($end_time);

2.获取指定日期的上个月色开始时间和结束时间

 1 <?php
 2  header('Content-Type: text/html; charset=utf-8');
 3 // 指定日期
 4 $date = '2015-01';
 5 // 获取指定日期的年、月
 6 $year = (int)date('Y',strtotime($date));
 7 $month = (int)date('m',strtotime($date));
 8 //  获取指定日期的上个月的开始时间和结束时间
 9  $beginLastMonthTime = date("Y-m-d",mktime(0, 0 , 0,($month-1),1,$year));
10  $endLastMonthTime = date("Y-m-d",mktime(23,59,59,$month ,0,$year));
11  var_dump($beginLastMonthTime);
12  var_dump($endLastMonthTime);

3.获取指定日期的前三个月的开始时间和结束时间

 1 <?php
 2 header('Content-Type: text/html; charset=utf-8');
 3 // 指定日期
 4 $date = '2015-05';
 5 // 获取指定日期的年、月
 6 $year = (int)date('Y',strtotime($date));
 7 $month = (int)date('m',strtotime($date));
 8 // 获取指定日期的前三个月的开始和结束时间
 9 $beginMonthTime = date("Y-m-d",mktime(0, 0 , 0,($month-3),1,$year));
10  $endMonthTime = date("Y-m-d",mktime(23,59,59,($month-2) ,0,$year));

4.判断某年某月有多少天

 1 <?php
 2  header('Content-Type: text/html; charset=utf-8');
 3  // 指定日期
 4  $date = ''2015-05'
 5  // 获取指定日期的年月
 6  $year = date('Y');
 7  $month = date('m');
 8  // 指定日为当月的第一天 
 9  $day = '01';  
10  //获取当年当月第一天的时间戳(时,分,秒,月,日,年)  
11  $timestamp = mktime(0,0,0,$month,$day,$year);  
12  $result = date('t',$timestamp);  

 

猜你喜欢

转载自www.cnblogs.com/honeyJYY/p/9327415.html