datetime - PHP. Why does date object diff on month reset to 0 after 12 months? -
today 2015-07-08.
when run following returns 0
. should not return 12
? date 12 months ago. if $date='2014-06-03'
returns 1
. should not 13
? dates within last 12 months calculated correctly.
$date = '2014-07-03'; $datetime1 = new datetime(date('y-m-01', strtotime($date))); $datetime2 = new datetime(date('y-m-d')); $interval = $datetime1->diff($datetime2); $months_ago = $interval->format('%m'); echo "\n".$months_ago;
thank you
the date 1 year
, 0 months
ago, not 12 months ago. if see year you'd that.
note:
the dateinterval::format() method not recalculate carry on points in time strings nor in date segments. expected because not possible overflow values "32 days" interpreted "1 month , 4 days" "1 month , 1 day".
you can use %a
total number of days can't find marker total number of months.
Comments
Post a Comment