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

yipeiwu_com5年前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如何搭建百度Ueditor富文本编辑器

PHP如何搭建百度Ueditor富文本编辑器

本文为大家分享了PHP搭建百度Ueditor富文本编辑器的方法,供大家参考,具体内容如下 下载UEditor 官网:下载地址 将下载好的文件解压到thinkphp项目中,本文是解压到PU...

CURL状态码列表(详细)

CURL状态码列表 状态码 状态原因 解释 0 正常访问 1 错误的协议 未支持的协议。此版cURL 不支持这一协议。 2 初始化代码失败 初始...

PHP中输出转义JavaScript代码的实现代码

分享一下: 复制代码 代码如下: function jsformat($str) { $str = trim($str); $str = str_replace('\\s\\s', '\...

简单谈谈PHP中的Reload操作

前言 有很多前辈告诫过我们,reload 能保证整个过程的平滑性,所谓平滑性指的是在 reload 的过程中,旧的进程在处理完当前请求前不会提前终止。很多年来,我从来没有质疑过这种说法,...

PHP实现数组的笛卡尔积运算示例

本文实例讲述了PHP实现数组的笛卡尔积运算。分享给大家供大家参考,具体如下: 数组的笛卡尔积在实际中还是挺有用处的,比如计算商品的规格时就经常用到,下面写一种实现方式,如下代码 $a...