note 49477 deleted from function.strtotime by tularis
| From: | tularis@php.net | Date: | Sat, 29 Jan 2005 19:19:43 +0000 |
| Subject: | note 49477 deleted from function.strtotime by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-84162@lists.php.net to get a copy of this message | ||
Note Submitter: ldbkutty at yahoo dot com
Reason: useless
----
Hi
I see there is no equivalent of Java's setLenient() method. If For ex., if you are on January
31st and do "+1 month" , you will get the result as 01st March.
I wrote one which works for adding any number of months to a date with Lenient as false.
<?php
/**
* @author Balakrishnan
*/
function addMonthNoLenient ($my_date, $add_month)
{
$day = date ( "d", $my_date );
$month = date ( "m", $my_date );
$year = date ( "Y", $my_date );
$new_date = date ( "Y-m-d", mktime( 0, 0, 0, $month + $add_month, $day, $year) );
$month_for_days = ( $month + $add_month ) % 12;
if( $month_for_days == 0 )
{
$month_for_days = 12;
}
if( $month_for_days != date ( "m", mktime( 0, 0, 0, $month + $add_month , $day, $year )
) )
{
$new_year = date ( "Y", mktime ( 0, 0, 0, $month + $add_month, $day, $year ) );
$days_in_month = cal_days_in_month ( CAL_GREGORIAN, $month_for_days, $new_year );
$new_date = date ( "Y-m-d", mktime ( 0, 0, 0, $month + $add_month, $days_in_month,
$year ) );
}
return $new_date;
}
?>
<?
$your_date = mktime ( 0, 0, 0, 1, 30, 2004 );
echo addMonthNoLenient ( $your_date, 22 );
?>