note 80769 modified in function.strtotime by danbrown
| From: | danbrown@php.net | Date: | Sun, 20 Jul 2008 04:04:12 +0000 |
| Subject: | note 80769 modified in function.strtotime by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-142107@lists.php.net to get a copy of this message | ||
More intelligent function to switch between months:
<?php
private function MoveMonth( $absolution, $timestamp )
{
$day = intval( date( "d", $timestamp ) );
$month = intval( date( "m", $timestamp ) );
$year = intval( date( "Y", $timestamp ) );
$hour = date( "H", $timestamp );
$minute = date( "i", $timestamp );
$second = date( "s", $timestamp );
// Positive movement
if( $absolution > 0 )
{
$absolution = abs( $absolution );
for( $abs=0;$abs<$absolution;$abs++ )
{
$month ++;
if( $month > 12 )
{
$month = 1;
$year ++;
}
}
}
// Negative movement
else if( $absolution < 0 )
{
$absolution = abs( $absolution );
for( $abs=0;$abs<$absolution;$abs++ )
{
$month --;
if( $month < 1 )
{
$month = 12;
$year --;
}
}
}
// Prevent from going over a month ( 31.01. --> 31.02. is impossible but 29.02. is ok )
$daysInMonth = date( 't', mktime( 0, 0, 0, $month, 1, $year ) );
if( $daysInMonth < $day )
$day = $daysInMonth;
return mktime( $hour, $minute, $second, $month, $day, $year );
}
?>
--was--
More intelligent function to switch between months:
private function MoveMonth( $absolution, $timestamp )
{
$day = intval( date( "d", $timestamp ) );
$month = intval( date( "m", $timestamp ) );
$year = intval( date( "Y", $timestamp ) );
$hour = date( "H", $timestamp );
$minute = date( "i", $timestamp );
$second = date( "s", $timestamp );
// Positive movement
if( $absolution > 0 )
{
$absolution = abs( $absolution );
for( $abs=0;$abs<$absolution;$abs++ )
{
$month ++;
if( $month > 12 )
{
$month = 1;
$year ++;
}
}
}
// Negative movement
else if( $absolution < 0 )
{
$absolution = abs( $absolution );
for( $abs=0;$abs<$absolution;$abs++ )
{
$month --;
if( $month < 1 )
{
$month = 12;
$year --;
}
}
}
// Prevent from going over a month ( 31.01. --> 31.02. is impossible but 29.02. is ok )
$daysInMonth = date( 't', mktime( 0, 0, 0, $month, 1, $year ) );
if( $daysInMonth < $day )
$day = $daysInMonth;
return mktime( $hour, $minute, $second, $month, $day, $year );
}
http://php.net/manual/en/function.strtotime.php