当前位置:Gxlcms > PHP教程 > php获取上月、本月、近15天、近30天的方法代码示例

php获取上月、本月、近15天、近30天的方法代码示例

时间:2021-07-01 10:21:17 帮助过:2人阅读

这篇文章主要介绍了PHP简单获取上月、本月、近15天、近30天的方法,结合实例形式分析了PHP通过自定义函数封装的日期与时间戳转换相关运算技巧,需要的朋友可以参考下

本文实例讲述了PHP简单获取上月、本月、近15天、近30天方法。分享给大家供大家参考,具体如下:

  1. /**
  2. * 获取统计时间
  3. * @param $type
  4. * 1 上月
  5. * 2 本月
  6. * 3 近15天
  7. * 4 近30天
  8. * @return array
  9. */
  10. function getDateInfo($type)
  11. {
  12. $data = array(
  13. array(
  14. 'firstday' => date('Ym01', strtotime('-1 month')),
  15. 'lastday' => date('Ymt', strtotime('-1 month')),
  16. ),
  17. array(
  18. 'firstday' => date('Ym01', strtotime(date("Y-m-d"))),
  19. 'lastday' => date('Ymd', strtotime((date('Ym01', strtotime(date("Y-m-d")))) . " +1 month -1 day")),
  20. ),
  21. array(
  22. 'firstday' => date('Ymd', strtotime("-15 day")),
  23. 'lastday' => date('Ymd', strtotime('-1 day')),
  24. ),
  25. array(
  26. 'firstday' => date('Ymd', strtotime("-30 day")),
  27. 'lastday' => date('Ymd', strtotime('-1 day')),
  28. ),
  29. );
  30. return is_null($type) ? $data : $data[$type-1];
  31. }
  32. print_r(getDateInfo(1));//获取上个月第一天与最后一天

运行结果:

  1. Array
  2. (
  3. [firstday] => 20170601
  4. [lastday] => 20170630
  5. )

以上就是php 获取上月、本月、近15天、近30天的方法代码示例的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行