date - How to get absolute number of months ago in PHP -


there number of questions regarding this, couldn't find exact answer.

$datetime1 = new datetime('2015-01-15'); $datetime2 = new datetime(date('y-m-d')); $interval = $datetime1->diff($datetime2); echo $interval->format('%m months'); 

as today 2015-05-11 returns 3 months. assume it's 1st day of month $datetime1 should return 4 months

i supposed use str_replace() or other string function lop off day part of $datetime1 i'm assuming there more elegant method?

thanks

if '2015-01-15' variable. do:

$date = '2015-01-15'; $datetime1 = new datetime( date('y-m-01', strtotime($date)) ); 

or use substring function.

$date = '2015-01-15'; $datetime1 = new datetime( substr($date, 0, 7).'-01' ); 

substring faster strtotime() can handle various formats.


Comments

Popular posts from this blog

android - MPAndroidChart - How to add Annotations or images to the chart -

javascript - Add class to another page attribute using URL id - Jquery -

firefox - Where is 'webgl.osmesalib' parameter? -