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

yipeiwu_com6年前PHP代码库

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

/**
 * 获取统计时间
 * @param $type
 * 1 上月
 * 2 本月
 * 3 近15天
 * 4 近30天
 * @return array
 */
function getDateInfo($type)
{
  $data = array(
    array(
      'firstday' => date('Ym01', strtotime('-1 month')),
      'lastday' => date('Ymt', strtotime('-1 month')),
    ),
    array(
      'firstday' => date('Ym01', strtotime(date("Y-m-d"))),
      'lastday' => date('Ymd', strtotime((date('Ym01', strtotime(date("Y-m-d")))) . " +1 month -1 day")),
    ),
    array(
      'firstday' => date('Ymd', strtotime("-15 day")),
      'lastday' => date('Ymd', strtotime('-1 day')),
    ),
    array(
      'firstday' => date('Ymd', strtotime("-30 day")),
      'lastday' => date('Ymd', strtotime('-1 day')),
    ),
  );
  return is_null($type) ? $data : $data[$type-1];
}
print_r(getDateInfo(1));//获取上个月第一天与最后一天

运行结果:

Array
(
  [firstday] => 20170601
  [lastday] => 20170630
)

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程序设计有所帮助。

相关文章

PHP中file_exists函数不支持中文名的解决方法

一般来说PHP中常使用file_exists()判断某个文件或者文件夹是否存在,如果存在则返回true,否则返回false。但是该函数在网页使用UTF8编码的情况下,对于中文的文件名或者...

php支持中文字符串分割的函数

str_split不支持中文,利用mb_xx函数实现个 /** * Convert a string to an array * @param string $str * @p...

PHP彩蛋信息介绍和阻止泄漏的方法(隐藏功能)

Easter Eggs(复活节彩蛋)外行人估计不了解这是神木玩意,彩蛋的网络解释是:用于电脑、电子游戏、电脑游戏、影碟或其他互动多媒体之中的隐藏功能或信息。PHP包含一个安全漏洞,可能导...

PHP单元测试框架PHPUnit用法详解

本文实例讲述了PHP单元测试框架PHPUnit用法。分享给大家供大家参考,具体如下: 以前在学习IOS开发时有专门写过Objective-C的单元测试的文章,IOS开发学习之单元测试,...

PHP中大括号'{}'用法实例总结

本文实例讲述了PHP中大括号'{}'用法。分享给大家供大家参考,具体如下: 在PHP中,大括号“{}”可以起到如下作用: 1、将多个独立语句合并为一个复合语句,例如 if ... els...