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

PHP简单获取上月、本月、近15天、近30天的方法示例

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

本文实例讲述了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. )

PS:这里再为大家推荐几款时间及日期相关工具供大家参考使用:

在线日期/天数计算器:
http://tools.jb51.net/jisuanqi/date_jisuanqi

在线日期计算器/相差天数计算器:
http://tools.jb51.net/jisuanqi/datecalc

在线日期天数差计算器:
http://tools.jb51.net/jisuanqi/onlinedatejsq

Unix时间戳(timestamp)转换工具:
http://tools.jb51.net/code/unixtime

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php日期与时间用法总结》、《PHP数组(Array)操作技巧大全》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

人气教程排行