php获取昨天/今天/明天/上周/本月/过去N月起止时间戳

PHP获取昨天、今天、明天、上周、本月、过去几/N个月、过去半年、一年后等起始时间戳和结束时间戳的方法

首先了解两个PHP函数:
strtotime()函数:将任何英文文本的日期时间描述解析为 Unix 时间戳

strtotime(time,now)


mktime() 函数:返回一个日期的 Unix 时间戳

mktime(hour,minute,second,month,day,year,is_dst)


php获取昨天 今天 明天 上周 本月 一年后 十年后的开始时间戳和结束时间戳:

  1. //php获取今天日期  
  2. date("Y-m-d");  
  3.   
  4. //php获取昨天日期  
  5. date("Y-m-d",strtotime("-1 day"))  
  6.   
  7. //php获取明天日期  
  8. date("Y-m-d",strtotime("+1 day"))  
  9.   
  10. //php获取一周后日期  
  11. date("Y-m-d",strtotime("+1 week"))  
  12.   
  13. //php获取一周零两天四小时两秒后时间  
  14. date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds"))  
  15.   
  16. //php获取下个星期四日期  
  17. date("Y-m-d",strtotime("next Thursday"))  
  18.   
  19. //php获取上个周一日期  
  20. date("Y-m-d",strtotime("last Monday"))  
  21.   
  22. //php获取一个月前日期  
  23. date("Y-m-d",strtotime("last month"))  
  24.   
  25. //php获取一个月后日期  
  26. date("Y-m-d",strtotime("+1 month"))  
  27.   
  28. //php获取十年后日期  
  29. date("Y-m-d",strtotime("+10 year"))  
  30.   
  31. //php获取今天起止时间戳  
  32. mktime(0,0,0,date('m'),date('d'),date('Y'));  
  33. mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;  
  34.   
  35. //php获取昨天起止时间戳  
  36. mktime(0,0,0,date('m'),date('d')-1,date('Y'));  
  37. mktime(0,0,0,date('m'),date('d'),date('Y'))-1;  
  38.   
  39. //php获取上周起止时间戳  
  40. mktime(0,0,0,date('m'),date('d')-date('w')+1-7,date('Y'));  
  41. mktime(23,59,59,date('m'),date('d')-date('w')+7-7,date('Y'));  
  42.   
  43. //php获取本月起止时间戳  
  44. mktime(0,0,0,date('m'),1,date('Y'));  
  45. mktime(23,59,59,date('m'),date('t'),date('Y'));  
  46. /**By:未来往事博客 https://www.fity.cn**/  


php获取过去半年/N个自然月每月的起止时间戳:

  1. /** 
  2.  * 计算过去N月每个自然月的起止时间戳,包括当前月份 
  3.  * @param  integer $max      月份数,默认为6 
  4.  * @param  string  $monthstr 返回值数组的第三个下标(具体月份的表示形式) 
  5.  * @return array            每个子数组形式array(月起始时间戳,月结束时间戳,月份名称) 
  6.  * @author            来源未来往事博客:https://www.fity.cn 
  7.  */  
  8. function month_offset($max= 6,$monthstr = 'ym'){  
  9.     if ($max<=0) {  
  10.         return false;  
  11.     }  
  12.     $base = date('Y-m-01');  
  13.     $y = 1;  
  14.     $mo = array();  
  15.     $mo[0][0] = strtotime($base);  
  16.     $mo[0][1] = time();  
  17.     $mo[0][2] = date($monthstr);  
  18.     while ($y < $max) {  
  19.         $mo[$y][0] = strtotime(date('Y-m-01',strtotime($base.' -'.$y.' month')));  
  20.         $mo[$y][1] = $y == 1?$mo[0][0]:$mo[$y-1][0];  
  21.         $mo[$y][2] = date($monthstr,$mo[$y][0]);  
  22.         $y++;  
  23.     }  
  24.     return array_reverse($mo);  
  25. }  
  26. 默认输出结果:  
  27. array (size=6)  
  28.   0 =>   
  29.     array (size=3)  
  30.       0 => int 1464710400  
  31.       1 => int 1467302400  
  32.       2 => string '1606' (length=4)  
  33.   1 =>   
  34.     array (size=3)  
  35.       0 => int 1467302400  
  36.       1 => int 1469980800  
  37.       2 => string '1607' (length=4)  
  38.   2 =>   
  39.     array (size=3)  
  40.       0 => int 1469980800  
  41.       1 => int 1472659200  
  42.       2 => string '1608' (length=4)  
  43. …………  


php获取两个日期之间的日期:

  1. /** 
  2.  * 计算过去N月每个自然月的起止时间戳,包括当前月份 
  3.  * @param  string  $start 开始日期,例如:2016-10-10 
  4.  * @param  string  $end 结束日期 
  5.  * @return string  日期 
  6.  * @author            来源未来往事博客:https://www.fity.cn 
  7.  */  
  8. function getDates($start$end) {   
  9.     $dt_start = strtotime($start);  
  10.     $dt_end   = strtotime($end);  
  11.     do {   
  12.         echo date('Y-m-d'$dt_start).' 
  13. ';  
  14.     } while (($dt_start += 86400) <= $dt_end);// 重复 Timestamp + 1 天(86400), 直至大于结束日期中止  
  15. }  
  16. getDates('2016-10-10','2016-12-09');  
  17. 默认输出结果:  
  18. 2016-10-10  
  19. 2016-10-11  
  20. 2016-10-12  
  21. 2016-10-13  
  22. 2016-10-14  
  23. 2016-10-15  
  24. …………  

本文最后更新于 2015-04-16 09:04:52 并被添加「php php基础」标签,已有 8473 位童鞋阅读过。
本文作者:未来往事
本站使用「署名 4.0 国际」创作共享协议,可自由转载、引用,但需署名作者且注明文章出处

相关文章

仅有 1 条评论
  1. 买木耳

    技术大牛

    买木耳 IP 属地:山西省 / 大同市

此处评论已关闭