Oli Filth wrote:
jo*********@sbcglobal.net said the following on 13/01/2006 18:34:
VS wrote:
jo*********@sbcglobal.net wrote:
lwoods wrote:
> I want to calculate and display the 'mm/dd/yyyy' that is 10 years
> earlier than today.
>
// configuration
$date = '01/01/2006';
$offset = '-10';
$date_glue = '/';
// implementation
$temp = explode($date_glue, $date);
$temp[2] = $temp[2] + $offset;
$date = implode ($date_glue, $date);
echo $date;
That technique only works for some date calculations.
Try to take off 10 days during the first 10 days of the month or 10
months between Jan and October and you'll get strange results.
Well, sure, but he was only asking about the year hehe. For the day
or month, a more advanced version would be required.
How about 29/02/2004?
hehe - Well, the title of the thread says mm/dd/yyyy. So, again, i was
just going by the data given by the thread starter. You can always
adapt your code to handle the format of dd/mm/yyyy. Something like this...
// configuration
$date = '01/01/2006';
$offset = '-10';
$date_glue = '/';
// change if your month is in a different position, such as dd/mm/yy
$month_location = '1';
$day_location = '2';
// implementation
$temp = explode($date_glue, $date);
if $temp[$day_location - 1] < 11
{
$eat_previous_month_by_this = 10 - $temp[$day_location - 1];
switch ($temp[$month_location - 1]) {
case 1:
$temp[$month_location - 1] = 12;
$temp[$day_location - 1] = 31 - $eat_previous_month_by_this;
break;
case 2:
$temp[$month_location - 1]--;
$temp[$day_location - 1] = 31 - $eat_previous_month_by_this;
break;
case 3:
$temp[$month_location - 1]--;
// check for leap year here. is year divisible by four? if so, then
use 29, if not then use 28. a simple if statement should suffice.
$temp[$day_location - 1] = 28 - $eat_previous_month_by_this;
break;
case 4:
$temp[$month_location - 1]--;
$temp[$day_location - 1] = 31 - $eat_previous_month_by_this;
break;
case 5:
$temp[$month_location - 1]--;
$temp[$day_location - 1] = 30 - $eat_previous_month_by_this;
break;
case 6:
$temp[$month_location - 1]--;
$temp[$day_location - 1] = 31 - $eat_previous_month_by_this;
break;
case 7:
$temp[$month_location - 1]--;
$temp[$day_location - 1] = 30 - $eat_previous_month_by_this;
break;
case 8:
$temp[$month_location - 1]--;
$temp[$day_location - 1] = 31 - $eat_previous_month_by_this;
break;
case 9:
$temp[$month_location - 1]--;
$temp[$day_location - 1] = 31 - $eat_previous_month_by_this;
break;
case 10:
$temp[$month_location - 1]--;
$temp[$day_location - 1] = 30 - $eat_previous_month_by_this;
break;
case 11:
$temp[$month_location - 1]--;
$temp[$day_location - 1] = 31 - $eat_previous_month_by_this;
break;
case 12:
$temp[$month_location - 1]--;
$temp[$day_location - 1] = 30 - $eat_previous_month_by_this;
break;
}
}
$temp[$day_location - 1] = $temp[$day_location - 1] + $offset;
$date = implode ($date_glue, $date);
echo $date;