note 55268 added to function.strtotime
| From: | faress at NOSPAMMING dot PLEASE dot gmail dot com | Date: | Thu, 28 Jul 2005 19:49:45 +0000 |
| Subject: | note 55268 added to function.strtotime | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-92670@lists.php.net to get a copy of this message | ||
This is an quick correction to ldbkutty at yahoo dot com
12-Apr-2005 06:21
he was trying to find a function that will let him add months to a date.
- tried
$myDate = strtotime("+1 month",mktime(0,0,0,1,31,2004));
This returns 2004-03-02 but I wanted the result as 2004-02-29. So, I noticed there is no equivalent
function like setLenient as false and he wrote one that I corrected a bit because my installation
did not have the cal_days_in_month() function (I used the date('t', $timestamp) to get the
days in months for a particular year) .
Hope this helps someone.
<?php
function addMonthNoLenient($my_date, $add_month)
{
$mid_month = 15 ;
$day = date( "d", $my_date );
$month = date( "m", $my_date );
$year = date( "Y", $my_date );
$hour = date( "H", $my_date );
$minute = date( "i", $my_date );
$second = date( "s", $my_date );
$new_date_timestamp = mktime( $hour, $minute, $second, $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( $hour, $minute, $second, $month + $add_month
, $day, $year ) ) )
{
$new_year = date( "Y", mktime( $hour, $minute, $second, $month + $add_month, $day,
$year ) );
$days_in_month = date('t', mktime( $hour, $minute, $second, $month_for_days,
$mid_month, $new_year ) );
$new_date_timestamp = mktime( $hour, $minute, $second, $month + $add_month, $days_in_month,
$year );
}
return $new_date_timestamp;
}
?>
<?
$your_date = mktime( 0, 0, 0, 1, 30, 2004 );
$months_to_add = 25;
echo date( "Y-m-d", addMonthNoLenient( $your_date, $months_to_add ));
?>
----
Submitter IP: 66.207.199.35
----
Manual Page -- http://www.php.net/manual/en/function.strtotime.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+55268
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+55268&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+55268&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+55268&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+55268&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+55268&report=yes&reason=non-english
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+55268&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+55268&report=yes
Search -- http://master.php.net/manage/user-notes.php