treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Date issue

  • 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
  • Give this a try:
    $d = date(\"d\")-1;
    $date = date(\"Y.m.\".$d);
    echo $date;
  • Wait, if it's a variable that's already set, I would use this function:
    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.
  • Thanks for the reply, would this code work if the date was the first and changing to the last day of the previous month?
  • Unfortunately it does not. But what you could do is set the timezone 24hrs backwards...
  • This function is a bit longer, but see if it works..

    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
  • 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.
    $date = \"1998-08-14\";
    $newdate = strtotime ( '-3 day' , strtotime ( $date ) ) ;
    $newdate = date ( 'Y-m-j' , $newdate );
    echo $newdate;

    I tried to make this work,
    $date = (\"01.03.2010\");
    $newdate = strtotime ( '+1 day' , strtotime ( $date ) ) ;
    $newdate = date ( 'd-m-Y' , $newdate );
    echo $newdate;

    But it fails miserably.
    Any ideas?
  • "thisishard" said:
    I tried to make this work,
    $date = (\"01.03.2010\");
    $newdate = strtotime ( '+1 day' , strtotime ( $date ) ) ;
    $newdate = date ( 'd-m-Y' , $newdate );
    echo $newdate;

    But it fails miserably.
    Any ideas?

    Works like a charm for me, what's your problem exactly?
  • On mine it echoes '02-01-1970' rather than '02-03-2010'.