note 84048 deleted from function.strtotime by derick
| From: | derick@php.net | Date: | Sun, 03 Aug 2008 11:52:12 +0000 |
| Subject: | note 84048 deleted from function.strtotime by derick | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-143106@lists.php.net to get a copy of this message | ||
Note Submitter: highermind.org
----
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));
}
}
?>