date() How i can get the yesterday date in php ? or the date of tomoroow?!

Discussion in 'PHP' started by redhits, Feb 26, 2010.

  1. #1
    date only provide current date, but how i can get what day was yesterday or what day will be in 2 days?!
     
    redhits, Feb 26, 2010 IP
  2. HostingProvider

    HostingProvider Active Member

    Messages:
    1,480
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    95
    #2
    The function's syntax is:
    date  ( string $format  [, int $timestamp  ] )
    PHP:
    So the below code will output tomorrow's date:
    echo date($format, time()+24*60*60*1);
    PHP:
    Where 1 is the number of days that you want it to give or take off the current date. Can also be a negative number as well.

    Hope that helps!
     
    HostingProvider, Feb 26, 2010 IP
  3. zahidislam

    zahidislam Peon

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Please use this function
    int mktime ([ int $hour = date("H") [, int $minute = date("i") [, int $second = date("s") [, int $month = date("n") [, int $day = date("j") [, int $year = date("Y") [, int $is_dst = -1 ]]]]]]] )
    this function will use for your requirements you can edit dates to this function
     
    zahidislam, Feb 28, 2010 IP
  4. Altari

    Altari Peon

    Messages:
    188
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Try strtotime. It recognizes short phrases to calculate prior or future dates. -1 day, +23 hours, +2 years, etc.
    $yesterday = strtotime("-1 day"); // returns UNIX time
    $yesterday = date("F d, Y", $yesterday); // returns Month dd, YYYY
    
    // or to consolidate it into one line
    $yesterday = date("F d, Y", strtotime("-1 day"));
    Code (php):
     
    Altari, Feb 28, 2010 IP
  5. killaklown

    killaklown Well-Known Member

    Messages:
    2,666
    Likes Received:
    87
    Best Answers:
    0
    Trophy Points:
    165
    #5
    you can also do something like this to get yesterday, - 1 year

    
    // Can have multiple strtotime in the same line
    echo date('F d, Y',strtotime("-1 Day",strtotime("-1 year")));
    
    PHP:
     
    killaklown, Feb 28, 2010 IP