Adding days to current date or predefined day in php (useful php code)

Adding days to a predefined date in php

$d = mktime(0,0,0,$month,$day,$year);
$end_date = date(“Y m d”,strtotime(“+2 days”,$d));

To add days to current date
date(‘Y-m-d’, strtotime(“+2 days”));

 

Advertisements

27 thoughts on “Adding days to current date or predefined day in php (useful php code)

  1. Another way of adding days to a predefined date:
    // $end is the end date
    // $start is the start date, i.e. 2008-09-17

    $end = date(“Y-m-d”, strtotime($start . “+2 day”));

  2. This was very useful to me. If i didn’t find this code i would probably have ended up writing a long code to do the same work, thanks!

  3. Hi, Im getting this error:
    A non well formed numeric value
    encountered in … on line 1000 2010-08-23
    and the code is date(‘Y-m-d’, strtotime($variable1->getCreateDate(“d.m.Y”), “+”.(integer)$variable2->getTerm()).” day”).

    The value of $variable2->getTerm() is 14. Do help. Thanking you in advance

  4. Work perfect for me TY Champ i use it for point system on my site

    Sample

    //get the order last update from database…. the last update use when i ship the order

    $result = mysql_query(“SELECT od_last_update FROM tbl_order WHERE od_id = $od_start”);
    while($row = mysql_fetch_array($result)){
    $odlastupdate = $row[‘od_last_update’];
    }

    //split the date result

    $date7 = substr(“$odlastupdate”, 0, 10);
    $dateArray=explode(‘-‘,$date7);
    $d = mktime(0,0,0,$dateArray[1],$dateArray[2],$dateArray[0]);

    //give 1 month to end…. after is reset the point

    $end_date = date(“d-m-Y”,strtotime(“+1 month”,$d));

    //Day to expyery;
    $todaydate = date(‘Y-m-d’);
    $today = strtotime(“$todaydate”);
    $expDateIs = strtotime(“$end_date”);

    printf(“you have %d day to use the point.”, round(abs($today-$expDateIs)/60/60/24));

  5. Hi,
    for me this code doing awesome job
    $prev_date = “2012-1-31”;
    $newdate = strtotime ( ‘1 day’ , strtotime ( $prev_date ) ) ;
    $newdate= date(‘Y-m-d’,$newdate);
    echo $newdate;

    Regard
    Sharjeel

  6. Nice, I didnt use it the way you did. I’m building graphs tracking customers per week/month year and I needed to say (if today is wednesday… weekstart is -2 days (monday)… etc for all 7 days. I was able to adapt what you showed me about strtotime(“-2 days -2 hours”) to accomplish this (server is two TZ’s ahead of me). WORKS LIKE A CHARM.

    $start_date = 0;
    $wd_today = Day4Date(undbdate(date(“Y-m-d”,time()-7200)));
    if ( $wd_today == “Sunday” ) {
    $start_date = date(“Y-m-d”,strtotime(“-6 days -2 hours”));
    } else if ( $wd_today == “Saturday” ) {
    $start_date = date(“Y-m-d”,strtotime(“-5 days -2 hours”));
    } else if ( $wd_today == “Friday” ) {
    $start_date = date(“Y-m-d”,strtotime(“-4 days -2 hours”));
    } else if ( $wd_today == “Thursday” ) {
    $start_date = date(“Y-m-d”,strtotime(“-3 days -2 hours”));
    } else if ( $wd_today == “Wednesday” ) {
    $start_date = date(“Y-m-d”,strtotime(“-3 days -2 hours”));
    } else if ( $wd_today == “Wednesday” ) {
    $start_date = date(“Y-m-d”,strtotime(“-2 days -2 hours”));
    } else if ( $wd_today == “Tuesday” ) {
    $start_date = date(“Y-m-d”,strtotime(“-1 days -2 hours”));
    } else if ( $wd_today == “Monday” ) {
    $start_date = date(“Y-m-d”,strtotime(“-2 hours”));
    }
    if ( $AllGet ) {
    if ( mysql_num_rows($AllGet) > 0 ) {
    while ( $single = mysql_fetch_array($AllGet) ) {
    if ( strtotime( $single[‘VisitDate’] ) >= strtotime($start_date) ) {
    $wtarray[0]+=1;
    //Find All Mondays
    if ( Day4Date(undbdate( $single[‘VisitDate’] )) == “Monday” ) {
    $warray[0] +=1;
    } else if ( Day4Date(undbdate( $single[‘VisitDate’] )) == “Tuesday” ) {
    $warray[1] +=1;
    } else if ( Day4Date(undbdate( $single[‘VisitDate’] )) == “Wednesday” ) {
    $warray[2] +=1;
    } else if ( Day4Date(undbdate( $single[‘VisitDate’] )) == “Thursday” ) {
    $warray[3] +=1;
    } else if ( Day4Date(undbdate( $single[‘VisitDate’] )) == “Friday” ) {
    $warray[4] +=1;
    } else if ( Day4Date(undbdate( $single[‘VisitDate’] )) == “Saturday” ) {
    $warray[5] +=1;
    } else if ( Day4Date(undbdate( $single[‘VisitDate’] )) == “Sunday” ) {
    $warray[6] +=1;
    }
    }
    }
    }
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s