note 84615 modified in function.strtotime by felipe

From: Date: Tue, 22 Jul 2008 11:55:12 +0000
Subject: note 84615 modified in function.strtotime by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-142487@lists.php.net to get a copy of this message
the post with the date_addmonths function doesn't really work, and its returning a timestamp not a date. i find the following function works well for me, to address the same problem. <?php # this function is needed for 31-jan-08 + 1 month to give 28-feb-08, etc. if (!function_exists('time_addmonths')) { function time_addmonths($n_months, $timestamp=null) { if (is_null($timestamp)) $timestamp=time(); $newt = strtotime("+$n_months months",$timestamp); $newd = date('d',$newt); if (date('d',$timestamp) != $newd) { # its rolled over to the next month so subtract the extra days $newt = strtotime("-$newd days",$newt); } return $newt; } } ?> --was-- the post with the date_addmonths function doesn't really work, and its returning a timestamp not a date. i find the following function works well for me, to address the same problem. # this function is needed for 31-jan-08 + 1 month to give 28-feb-08, etc. if (!function_exists('time_addmonths')) { function time_addmonths($n_months, $timestamp=null) { if (is_null($timestamp)) $timestamp=time(); $newt = strtotime("+$n_months months",$timestamp); $newd = date('d',$newt); if (date('d',$timestamp) != $newd) { # its rolled over to the next month so subtract the extra days $newt = strtotime("-$newd days",$newt); } return $newt; } } http://php.net/manual/en/function.strtotime.php

« previous php.notes (#142487) next »