ユーザ用ツール

サイト用ツール


php_datetime

DateTime

-1 month

  • 月が遡るわけではなく、30日だけ引き算するだけっぽい
Be aware of this behaviour:
 
<?php
$dt1 = new \DateTime( '2014/12/31' );
$dt1->modify( '-1 month' );
$m = (int) $dt1->format( 'm' );
var_dump( $m ); // still 12 !!!
 
$dt2 = new \DateTime( '2014/12/30' );
$dt2->modify( '-1 month' );
$m = (int) $dt2->format( 'm' );
var_dump( $m ); // 11
?>
 
The method modify( '-1 month' ) appear to remove 30 days, not to subtract 1 from the month and adjust the day (if needed).
 
In my opinion, the former date should be adjusted to 2014/11/30, that is, the last day in the previous month.

参考

php_datetime.txt · 最終更新: 2021/06/15 22:55 by 127.0.0.1