if you create a date (i.e. a certain day, like 30.03.2005, for a calendar for example) for which you do not consider the time, when using mktime be sure to set the time of the day to noon:strtotime("-1 month",mktime(12,0,0,1,31,2004)); ?>will cause troubles when calculating the relative time. It often is one day or even one month off... After I set the time to noon "strtotime" calculates as expected.Also taken from there:Just pointing out that date( 'M', strtotime( 'last month', [some timestamp]) ) will not actually give the name of the previous month if the day of [some timestamp] doesn't exsist in the previous month. It will instead round up, and give the name of one month AFTER the previous month ('this month', that is).For example date( 'M', strtotime( 'last month', [March 31st]) ) will return 'Mar'.This is documented here: http://www.gnu.org/software/tar/manual/html_chapter/tar_7.html (also linked from this manual page), but is not easy to find:" The fuzz in units can cause problems with relative items. For example, `2003-07-31 -1 month' might evaluate to 2003-07-01, because 2003-06-31 is an invalid date. To determine the previous month more reliably, you can ask for the month before the 15th of the current month. For example:$ date -R Thu, 31 Jul 2003 13:02:39 -0700 $ date --date='-1 month' +'Last month was %B?' Last month was July? $ date --date="$(date +%Y-%m-15) -1 month" +'Last month was %B!' Last month was June! "What you can do is do it manually:<?php $month = 31 * 24 * 60 * 60; //not exactly last month, but 31 days earlier. // 31 days; 24 hours; 60 minutes; 60 seconds $lastMonth = date('Y-m-d', time() - $month); ?>
对,这个bug已经存在很长时间了,具体可以到官网上去查.
但官方认为这不是BUG,建议用date("Y.m",strtotime("-31 days"))来替代.
要不就直接计算天数,减天数来做.
$j=$i*31;
date("Y.m",strtotime("-"."$j"."days"));
如果是月份数大了,比如说递减的月份到几年前了,哪要是按每月31天减也会产生误差吧,不到3年的周期会差出一个月吧。
直接用mysql的日期函数来做,可能会更好些.
比如说你想取08年的,哪就匹配一下 2008-xx的串吗,
mysql支持unix时间戳的操作.
这样比你事先构造串不是更好一些. 如果你非得想得到,也可以用select distinct()来实现呀.多一部查询而矣.
文字串转换时间不精确
select distinct(DATE_FORMAT('时间字段', '%Y%m')) FROM 表名
?>will cause troubles when calculating the relative time. It often is one day or even one month off... After I set the time to noon "strtotime" calculates as expected.Also taken from there:Just pointing out that date( 'M', strtotime( 'last month', [some timestamp]) ) will not actually give the name of the previous month if the day of [some timestamp] doesn't exsist in the previous month. It will instead round up, and give the name of one month AFTER the previous month ('this month', that is).For example date( 'M', strtotime( 'last month', [March 31st]) ) will return 'Mar'.This is documented here: http://www.gnu.org/software/tar/manual/html_chapter/tar_7.html (also linked from this manual page), but is not easy to find:" The fuzz in units can cause problems with relative items. For example, `2003-07-31 -1 month' might evaluate to 2003-07-01, because 2003-06-31 is an invalid date. To determine the previous month more reliably, you can ask for the month before the 15th of the current month. For example:$ date -R
Thu, 31 Jul 2003 13:02:39 -0700
$ date --date='-1 month' +'Last month was %B?'
Last month was July?
$ date --date="$(date +%Y-%m-15) -1 month" +'Last month was %B!'
Last month was June! "What you can do is do it manually:<?php
$month = 31 * 24 * 60 * 60; //not exactly last month, but 31 days earlier.
// 31 days; 24 hours; 60 minutes; 60 seconds
$lastMonth = date('Y-m-d', time() - $month);
?>