Robson » Little Red Book » Tomorrow's date

Chapter 2 - Question 9

Given today's date in the form day, month, year, output tomorrow's date in the same format.

Solution 1

<?
   // today's day, month and year
   $day = date(d);
   $month = date(m);
   $year = date(Y);
   
   // work out the timestamp of tomorrows date
   // if this is the last day of month/year, it will loop round to the next month/year
   $seconds = mktime(1,1,1,$month,$day+1,$year);
 
   // output today and tomorrow
   echo 'Today is ' . date('F jS, Y') . ', so tomorrow is ' . date('F jS, Y', $seconds);
?>

Which produces:

Today is September 20th, 2017, so tomorrow is September 21st, 2017

Solution 2

The strtotime() function is really useful for working out problems like this:

<?
   // output today and tomorrow
   echo 'Today is ' . date('F jS, Y') . ', so tomorrow is ' . date('F jS, Y', strtotime('Tomorrow'));
?>

Which produces:

Today is September 20th, 2017, so tomorrow is September 21st, 2017

Log

© 2004-17 robson | cc unless stated