Advertisements

*a constant parallactic paradox, sublime*

Dates in PHP (Tomorrow, Yesterday, Today of a Given Date)

<?php
echo “<h4>Date Yesterday</h4>”;
$yesterday = date(“Y-m-d”, time()-86400);
echo $yesterday;

echo “<h4>Date Today</h4>”;
echo date(“Y-m-d”);

echo “<h4>Date Tomorrow</h4>”;
$tomorrow = date(“Y-m-d”, time()+86400);
echo $tomorrow;

echo “<hr>”;
echo “<h4>Previous Date from User-defined Date</h4>”;

$gdate = “2008-07-11”;
echo “(” . $gdate . ” supplied.) <br>”;
$dt = strtotime($gdate);
echo date(“Y-m-d”, $dt-86400);

echo “<h4>Next Date from User-defined Date</h4>”;
echo “(” . $gdate . ” supplied.) <br>”;
$dt = strtotime($gdate);
echo date(“Y-m-d”, $dt+86400);

// DAYS IN BETWEEN TWO DATES

function days_in_between($s, $e){
$d = array();
$s = strtotime($s);
$e = strtotime($e);
for($n=$s; $n<=$e; $n=$n+86400){
array_push($d, strftime(“%Y-%m-%d”, $n));
}
return $d;

}

?>

Advertisements

9 responses

  1. DIsha

    Nice !

    17. December 2008 at 05:05

  2. you could also generate the date and time for tomorrow and yesterday by strtotime(“yesterday”); or strtotime(“tomorrow”);
    For this you get the time portion for 00:00 o’clock then.

    Be careful with just adding 86400 to the current timestamp. On days with a summer-winter-time change you can get false results because of the difference of one hour.

    27. February 2009 at 12:48

    • Yes, you are right. Thank you for the advice, it’s well appreciated.

      20. March 2009 at 06:57

  3. really useful tuts.
    thanks.

    12. November 2009 at 01:07

  4. thanks. you saved my day.

    29. December 2011 at 09:31

  5. CV

    Very useful, thanks!

    5. February 2012 at 06:30

  6. bubba

    no thinking and stealing your code saves me time and frustation!!! thanx!!! yeay for compute tomorrows date FREE AGLORITHM

    6. March 2012 at 13:45

  7. tomekmad

    NOPE NOPE NOPE. In your locale you may have run afoul of the daylight savings time change. Adding 86400 is not reliable for adding days!

    15. March 2013 at 15:59

    • yup someone addressed this issue already in the above comments.

      7. May 2013 at 05:49

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