note 95213 deleted from function.strtotime by derick

From: Date: Sun, 03 Nov 2013 15:21:38 +0000
Subject: note 95213 deleted from function.strtotime by derick
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-197410@lists.php.net to get a copy of this message
Note Submitter: emig647 at yahoo.com ---- Some have added their fixes for adding 1 month to a time. The problem I find with other fixes is if you are on the 30th (10-30-09) of a month that isn't the last day and add one month to their time, it goes to 1 day less than the last day of the previous month's day. (11-29-09). Here is my fix for adding 1 month so it's equal to of the previous month day or the last day of the month if it's less than the previous month day. <?php $anniversary = "2008-09-30"; $newChargeDate = strtotime($anniversary); $newChargeDate = addRealMonth($newChargeDate); print date("Y-m-j", $newChargeDate); ////////////////////////////////////////////////////// // Get the last day of the month function lastDayOfMonth($month = '', $year = '') { if (empty($month)) { $month = date('m'); } if (empty($year)) { $year = date('Y'); } $result = strtotime("{$year}-{$month}-01"); $result = strtotime('-1 second', strtotime('+1 month', $result)); return date('Y-m-d', $result); } function addRealMonth($timeStamp) { // Check if it's the end of the year and the month and year need to be changed $tempMonth = date('m', $timeStamp); $tempYear = date('Y', $timeStamp); if($tempMonth == "12") { $tempMonth = 1; $tempYear++; } else $tempMonth++; $newDate = lastDayOfMonth($tempMonth, $tempYear); return strtotime($newDate); } ?> This will output 2008-10-30. Or 2009-02-28 if the $anniversary is 2009-01-30. Hope this helps!

« previous php.notes (#197410) next »