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;
}
?>






Nice !
December 17, 2008 at 5:05 am
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.
February 27, 2009 at 12:48 pm
Yes, you are right. Thank you for the advice, it’s well appreciated.
March 20, 2009 at 6:57 am
really useful tuts.
thanks.
November 12, 2009 at 1:07 am
thanks. you saved my day.
December 29, 2011 at 9:31 am
Very useful, thanks!
February 5, 2012 at 6:30 am
no thinking and stealing your code saves me time and frustation!!! thanx!!! yeay for compute tomorrows date FREE AGLORITHM
March 6, 2012 at 1:45 pm