Grow your CSS skills. Land your dream job.

Date issue

  • # 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 3:10 am

    Thanks for the reply, would this code work if the date was the first and changing to the last day of the previous month?

    # 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 3:31 pm

    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.

    Code:
    $date = “1998-08-14″;
    $newdate = strtotime ( ‘-3 day’ , strtotime ( $date ) ) ;
    $newdate = date ( ‘Y-m-j’ , $newdate );
    echo $newdate;

    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?

    # 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?

    # March 12, 2010 at 4:22 pm

    On mine it echoes ’02-01-1970′ rather than ’02-03-2010′.

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

You must be logged in to reply to this topic.

*May or may not contain any actual "CSS" or "Tricks".