Ok, I have a variable which is a date, "2010.03.09" - yyyy.mm.dd I want to subtract one day from it and echo out "2010.03.08" or what ever the value is from subtracting one from the date given. Any help appreciated Thanks
I'd hate to be a pain but.... what about leap years :/ sorry. As a side not i found this bit of code which seems to do the trick for the guy who made it but i cant get it to work.
I want to subtract one day from it and echo out "2010.03.08" or what ever the value is from subtracting one from the date given.
Any help appreciated
Thanks
function fix_date($var){$var = explode(\".\", $var);
$day = $var[2];
$day = $day-1;
while(strlen($day)<2){
$day = '0'.$day;
}
$var = $var[0].'.'.$var[1];
$var .= '.'.$day;
echo $var;
}
fix_date('2010.03.09');
There's probably easier ways to go about doing this, but this is what I would do.
function fix_date($var){
$var = explode(\".\", $var);
$jan = \"31\";
if(date(\"L\") == 1){
$feb = 29;
}else{
$feb = 28;
}
$mar = 31;
$apr = 30;
$may = 31;
$jun = 30;
$jul = 31;
$aug = 31;
$sep = 30;
$oct = 31;
$nov = 30;
$dec = 31;
$year = $var[0];
$month = $var[1];
$day = $var[2];
$day = $day-1;
if($day < 1){
$month = $month-1;
if($month > 1){
if($month == 1){
$day = $jan;
}elseif($month == 2){
$day = $feb;
}elseif($month == 3){
$day = $mar;
}elseif($month == 4){
$day = $apr;
}elseif($month == 5){
$day = $may;
}elseif($month == 6){
$day = $jun;
}elseif($month == 7){
$day = $jul;
}elseif($month == 8){
$day = $aug;
}elseif($month == 9){
$day = $sep;
}elseif($month == 10){
$day = $oct;
}elseif($month == 11){
$day = $nov;
}elseif($month == 12){
$day = $dec;
}
}else{
$month = 12;
$year = $year-1;
$day = $dec;
}
}
while(strlen($day)<2){
$day = '0'.$day;
}
while(strlen($month)<2){
$month = '0'.$month;
}
$var = $year . '.' . $month . '.' . $day;
echo $var;
}
fix_date('2010.03.09');
If the day is january 1st, 2010 it will output: 2009.12.31
sorry.
As a side not i found this bit of code which seems to do the trick for the guy who made it but i cant get it to work.
I tried to make this work,
But it fails miserably.
Any ideas?
Works like a charm for me, what's your problem exactly?