strtotime('-x month'); 在涉及到月份修改的时候,可能不会得到预料的结果。
此为php的一个bug:
https://bugs.php.net/bug.php?id=27793
如:当前时间为: 2011-08-31 17:21:22
<?php
date_default_timezone_set('Asia/Shanghai');
$t = time();
print_r(array(
date('Y年m月',$t),
date('Y年m月',strtotime('- 1 month',$t)),
date('Y年m月',strtotime('- 2 month',$t)),
));
?>
上面代码输出:
Array
(
[0] => 2011年08月
[1] => 2011年07月
[2] => 2011年07月
)
而预期的结果是:
Array
(
[0] => 2011年08月
[1] => 2011年07月
[2] => 2011年06月
)
============================================
可以用如下方法解决:
<?php
date_default_timezone_set('Asia/Shanghai');
$first_day_of_month = date('Y-m',time()) . '-01 00:00:01';
$t = strtotime($first_day_of_month);
print_r(array(
date('Y年m月',$t),
date('Y年m月',strtotime('- 1 month',$t)),
date('Y年m月',strtotime('- 2 month',$t)),
));
?>
输出:
Array
(
[0] => 2011年08月
[1] => 2011年07月
[2] => 2011年06月
)
分享到:
相关推荐
在php中我想要获取时间戳有多种方法,最常用的就是使用time函数与strtotime()函数把日期转换成时间戳了,下面我来给大家分享一下时间戳函数 strtotime用法。获取指定的年月日转化为时间戳:pHP时间戳函数获取指定...
document.write(strtotime('last month')); document.write(strtotime('+1 weeks')); document.write(strtotime('+1 WEEK')); document.write(strtotime('+8 Days')); document.write(strtotime('-3 Hours 2 seconds...
PHP中strtotime函数使用方法.docx
如何使用PHP计算上一个月的今天>一文中, 我们提到strtotime函数在使用strtotime(”-1 month”)求上一个月的今天时会出一些状况,因此也引出写这篇文章,本文包括如下内容:•strtotime函数的一些用法•strtotime...
主要介绍了PHP strtotime函数用法、实现原理和源码分析,本文讲解了strtotime函数的一些用法、strtotime函数的实现基本原理、strtotime(“-1 month”)求值失败的原因等内容,需要的朋友可以参考下
使用strtotime可以将各种格式的时间字符串转换为时间戳 转换常规时间格式 echo date('Y-m-d H:i:s', strtotime('2016-01-30 18:00')).PHP_EOL; echo date('Y-m-d H:i:s', strtotime('20160130180001')).PHP_EOL; ...
一,获取指定日期的unix时间戳 strtotime(“2009-1-22”) 示例如下: 1.echo strtotime(“2009-1-22”) 结果:1232553600 说明:返回2009年1月22日0点0分0秒时间戳 二,获取英文文本日期时间 示例如下: 便于比较,...
在PHP中有个叫做strtotime的函数。strtotime 实现功能:获取某个日期的时间戳,或获取某个时间的时间戳。strtotime 将任何英文文本的日期时间描述解析为Unix时间戳[将系统时间转化成unix时间戳] 一,获取指定日期的...
先看手册介绍: strtotime — 将任何英文文本的日期时间描述解析为 Unix 时间戳 格式:int strtotime ( string $time [, int $now ] ) 本函数预期接受一个包含美国英语日期格式的字符串并尝试将其解析为 Unix 时间戳...
节点StrToTime函数。 这段代码是由从。 这是原始代码的简单分支,但主要区别在于未扩展Date.prototype范围。 例子 var strtotime = require('strtotime'); strtotime('1 week'); strtotime('next friday'); ...
今天写程序的时候,突然发现了很早以前写的获取月份天数的函数,经典的switch版,但是获得上月天数的时候,我只是把月份-1了,估计... $lastday = date("Y-m-d",strtotime("$firstday +1 month -1 day")); return ar
strtotime()是php中的时间函数;其功能是:将任何字符串形式的日期,时间转换成对应的Unix 时间戳。今天我们是通过具体的实例来详细分析下strtotime()函数的性能问题
本文实例讲述了php使用date和strtotime函数输出指定日期的方法。分享给大家供大家参考。具体方法分析如下: 在php中date和strtotime函数都是对日期操作的,但是在生成上面date和strtotime是不一样的,一个是数字日期...
主要介绍了php中strtotime函数的用法,以实例形式详细分析了strtotime函数的参数含义与相应的用法,适用于时间格式变换的情况,需要的朋友可以参考下
本文实例讲述了PHP中时间加减函数strtotime用法。分享给大家供大家参考,具体如下: 时间加减 <?php //获取本地 提取年份+1 $date=date("Y-m-d",mktime(0,0,0,date("m") ,date("d"),date("Y")+1)); ?> 如果...
其实就是strtotime这个内置函数 //PHP 日期 加减 周 date("Y-m-d",strtotime("2013-11-12 +1 week")) //PHP 日期 加减 天数 date("Y-m-d",strtotime("2013-11-12 12:12:12 +1 day")) //PHP 日期加减小时 date("Y-m-...