note 49477 added to function.strtotime
| From: | ldbkutty at yahoo dot com | Date: | Sat, 29 Jan 2005 18:57:38 +0000 |
| Subject: | note 49477 added to function.strtotime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-84161@lists.php.net to get a copy of this message | ||
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 );
?>
----
Manual Page -- http://www.php.net/manual/en/function.strtotime.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+49477
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+49477&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49477&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+49477&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+49477&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+49477&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+49477&report=yes
Search -- http://master.php.net/manage/user-notes.php