note 84048 modified in function.strtotime by danbrown

From: Date: Sun, 20 Jul 2008 03:53:26 +0000
Subject: note 84048 modified in function.strtotime by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-142097@lists.php.net to get a copy of this message
Concerning the "+1 month == 30.5 days" issue: The developer hubris surrounding this in the bug database is annoying: irregardless of how GNU date works there appears to be a genuine business need that adding one month to Jan 31 gives Feb 28 and not March 2. Think of an invoicing system for a service provider: if a customer's billing date is usually the 31st, having February's bill due on March 2nd will look out of place on a report, and your customers/employer/etc. will blame you, not GNU date. Also, the php 5.3 "last day of next month" $time string does not appear relevant. What is really needed is a separate string that can take a dynamic number of months and give us the date for the next month as humans would understand it: "+{$months} realmonth" or "+{$months} manmonth" or some such. In lieu of that, here are two functions to add and subtract months no matter the end of month date. <?php if (!funtion_exists('date_addmonths')) { function date_addmonths($n_months, $timestamp=null) { if (is_null($timestamp)) $timestamp=time(); $timestamp_midmonth = mktime(12, 0, 0, date('n', $timestamp)+$n_months, 15, date('Y', $timestamp)); return date('t', $timestamp) > date('t', $timestamp_midmonth) ? strtotime(date('t F Y', $timestamp_midmonth)) : strtotime(date(date('d', $timestamp).' F Y', $timestamp_midmonth)); } } if (!funtion_exists('date_submonths')) { function date_submonths($n_months, $timestamp=null) { if (is_null($timestamp)) $timestamp=time(); $timestamp_midmonth = mktime(12, 0, 0, date('n', $timestamp)-$n_months, 15, date('Y', $timestamp)); return date('t', $timestamp) > date('t', $timestamp_midmonth) ? strtotime(date('t F Y', $timestamp_midmonth)) : strtotime(date(date('d', $timestamp).' F Y', $timestamp_midmonth)); } } ?> --was-- Concerning the "+1 month == 30.5 days" issue: The developer hubris surrounding this in the bug database is annoying: irregardless of how GNU date works there appears to be a genuine business need that adding one month to Jan 31 gives Feb 28 and not March 2. Think of an invoicing system for a service provider: if a customer's billing date is usually the 31st, having February's bill due on March 2nd will look out of place on a report, and your customers/employer/etc. will blame you, not GNU date. Also, the php 5.3 "last day of next month" $time string does not appear relevant. What is really needed is a separate string that can take a dynamic number of months and give us the date for the next month as humans would understand it: "+{$months} realmonth" or "+{$months} manmonth" or some such. In lieu of that, here are two functions to add and subtract months no matter the end of month date. if (!funtion_exists('date_addmonths')) { function date_addmonths($n_months, $timestamp=null) { if (is_null($timestamp)) $timestamp=time(); $timestamp_midmonth = mktime(12, 0, 0, date('n', $timestamp)+$n_months, 15, date('Y', $timestamp)); return date('t', $timestamp) > date('t', $timestamp_midmonth) ? strtotime(date('t F Y', $timestamp_midmonth)) : strtotime(date(date('d', $timestamp).' F Y', $timestamp_midmonth)); } } if (!funtion_exists('date_submonths')) { function date_submonths($n_months, $timestamp=null) { if (is_null($timestamp)) $timestamp=time(); $timestamp_midmonth = mktime(12, 0, 0, date('n', $timestamp)-$n_months, 15, date('Y', $timestamp)); return date('t', $timestamp) > date('t', $timestamp_midmonth) ? strtotime(date('t F Y', $timestamp_midmonth)) : strtotime(date(date('d', $timestamp).' F Y', $timestamp_midmonth)); } } http://php.net/manual/en/function.strtotime.php

« previous php.notes (#142097) next »