PHP中使用strtotime函数注意事项
今天在微博上看到一个小案例,命令行如下:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5325')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5325>php -r “echo date(‘Y/m’,strtotime(‘-2 months’)) . \”\n\”;”
输出结果如下:
2013/03
如果把命令行修改成如下:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5944')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5944>php -r “echo date(‘Y/m’,strtotime(‘-3 months’)) . \”\n\”;”
输出结果如下:
2013/03
这里就奇怪了,怎么两个月前和三个月前的年月份都是一样的,再看看下面的命令行:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy2236')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy2236>php -r “echo date(‘Y/m’,strtotime(‘-3 months’,strtotime(’2013/05/28′))) . \”\n\”;”
输出结果如下:
2013/02
如果修改命令行为如下:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy1950')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy1950>php -r “echo date(‘Y/m’,strtotime(‘-3 months’,strtotime(’2013/05/29′))) . \”\n\”;”
输出结果如下:
2013/03
下面这个与预期的结果是一样的,现在说说为什么会出现开始的那个奇怪的坑。
strtotime里的months和 month是30天,使用的时候需要考虑 有31天和 2月份的情况,所以,如果没有考虑这个特殊的情况,那么在每月的31号或者和2月份有关的时间点时候就可能出现开始的那个灵异情况,这个算是strtotime的一个坑吧,为了慎重起见,能不用的时候就尽量别去用这个坑人的东东。
PS:以上所有例子都是在Debian6的64位系统下执行,使用命令行方式执行,执行时间是2013-05-30.
上月下月时间不准确
以下有几种方法,可以帮助我们达到预期效果,比如我要返回上个月的月份:
或者:
echo date('M Y', strtotime(date('Y-m-01')) - 86400);
下方是其他的用途:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5304')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5304>strtotime('first day of last month');
strtotime('last day of last month');
strtotime('first of last week');
strtotime('first of this week');
strtotime('this week midnight'); // returns Monday midnight of this week
strtotime('last week midnight'); // returns Monday midnight of last week
strtotime('last week Sunday midnight'); // returns Sunday midnight of this week
strtotime('-2 weeks Sunday midnight'); // returns Sunday midnight of last week
<?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)),
));
?>
您可能感兴趣的文章:
PHP中使用strtotime函数注意事项
如何实现转换php时间戳
php时间函数strtotime的深入理解
php字符串转时间戳
php如何将日期转换为时间戳
PHP常用日期时间操作合集
php strtotime函数怎么用
php中的UNIX时间戳函数strtotime
php时间戳转换
深入php日期函数的应用实例详解