Forums

Give help. Get help.

  • # March 9, 2010 at 3:41 pm

    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

    # March 9, 2010 at 10:35 pm

    Give this a try:

    Code:
    $d = date(“d”)-1;
    $date = date(“Y.m.”.$d);
    echo $date;
    # March 9, 2010 at 10:47 pm

    Wait, if it’s a variable that’s already set, I would use this function:

    Code:
    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.

    # March 10, 2010 at 9:25 pm

    Unfortunately it does not. But what you could do is set the timezone 24hrs backwards…

    # March 11, 2010 at 12:22 am

    This function is a bit longer, but see if it works..

    Code:
    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

    # March 11, 2010 at 5:24 pm
    "thisishard" wrote:
    I tried to make this work,

    Code:
    $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?

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.

icon-anchoricon-closeicon-emailicon-linkicon-logo-staricon-menuicon-nav-guideicon-searchicon-staricon-tag