ホームページ  >  記事  >  バックエンド開発  >  strtotime(" x month") のバグに対する PHP の解決策

strtotime(" x month") のバグに対する PHP の解決策

WBOY
WBOYオリジナル
2016-06-13 13:12:101030ブラウズ

strtotime("- x month") のバグに対する PHP ソリューション


strtotime('-x month'); 月の変更に関しては、期待した結果が得られない可能性があります。

これは php のバグです:

https://bugs.php.net/bug.php?id=27793

?

例: 現在時刻: 2011-08-31 17:21:22

date_default_timezone_set('アジア/上海');
$t = 時間();
print_r(array(
??? ??? 日付('Y年m月',$t),
??? ??? date('Y 年 m 月',strtotime('- 1 か月',$t)),
??? ??? date('Y 年 m 月',strtotime('-2 か月',$t)),
));

?>

上記のコードの出力:

配列
(
??? [0] => 2011 年 8 月
??? [1] => 2011 年 7 月
??? [2] => 2011 年 7 月
)

期待される結果は次のとおりです:

配列
(
??? [0] => 2011 年 8 月
??? [1] => 2011 年 7 月
??? [2] => 2011 年 6 月
)

?

==============================================

?

次の方法を使用して解決できます:

date_default_timezone_set('アジア/上海');

$first_day_of_month = date('Y-m',time()) '-01 00:00:01';
$t = strtotime($first_day_of_month);
print_r(array(
??? ??? 日付('Y年m月',$t),
??? ??? date('Y 年 m 月',strtotime('- 1 か月',$t)),
??? ??? date('Y 年 m 月',strtotime('- 2 月',$t),
));

?>

出力:

配列
(
??? [0] => 2011 年 8 月
??? [1] => 2011 年 7 月
??? [2] => 2011 年 6 月
)

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。