<p>I'm trying to use <code>strtotime()</code> to respond to a button click to advance -1 and +1 days (two buttons) relative to the day advanced to on the previous click.  </p>

<p><strong>Example:</strong>  </p>

<ul>
<li>It's the 10th of the month, I click "-1 day" button, and now the date reads as the 9th.  </li>
<li>I click the "-1 day" button again and now the readout states the 8th day. </li>
<li>I click the "+1 day" button and now the readout states it's the 9th.  </li>
</ul>

<p>I understand the buttons and the displaying the date and using <code>$_GET</code> and PHP to pass info, but how do I get <code>strtotime()</code> to work on the relative date from the last time the time travel script was called?</p>

<p>My work so far has let me show yesterday and today relative to <em>now</em> but not relative to, for example, the <em>day before yesterday</em>, or the <em>day after tomorrow</em>.  Or if I use my <strong>"last monday"</strong> button, the day before or after whatever that day is.</p>

[{'answer_id': 8689, 'author': 'Philip Reynolds', 'author_id': 1087, 'author_profile': 'https://Stackoverflow.com/users/1087', 'pm_score': 1, 'selected': False, 'text': '<p>Kevin, you work off a solid absolute base (i.e. a date / time), not a relative time period. You then convert to the relative time periods. So, for example, by default, if you were showing a calendar, you\'d work from todays date. </p>\n\n<pre><code>int strtotime  ( string $time  [, int $now  ] )\n</code></pre>\n\n<p>You can see in the function definition here of strtotime, the second argument is now, i.e. you can change the date from which it\'s relative.</p>\n\n<p>This might be easier to display through a quick loop</p>\n\n<p>This will loop through the last 10 days using "yesterday" as the first argument. We then use date to print it out.</p>\n\n<pre><code>$time = time();\n\nfor ($i = 0; $i &lt; 10; $i++) {\n    $time = strtotime("yesterday", $time);\n    print date("r", $time) . "\\n";\n}\n</code></pre>\n\n<p>So pass the time/date in via the URI so you can save the relative date. </p>\n'}, {'answer_id': 11402, 'author': 'kevtrout', 'author_id': 1149, 'author_profile': 'https://Stackoverflow.com/users/1149', 'pm_score': 0, 'selected': False, 'text': '<p>After a moment of inspiration, the solution to my question became apparent to me (I was riding my bike).  The \'$now\'  part of </p>\n\n<pre><code>strtottime( string $time {,int $now ]) \n</code></pre>\n\n<p>needs to be set as the current date.  Not "$time()-now", but "the current date I\'m concerned with / I\'m looking at my log for.</p>\n\n<p>ie: if I\'m looking at the timesheet summary for 8/10/2008, then that is "now" according to strtotime(); yesterday is 8/09 and tomorrow is 8/11.  Once I creep up one day, "now" is 8/11, yesterday is 8/10, and tomorrow is 8/12.</p>\n\n<p>Here\'s the code example:</p>\n\n<pre><code>&lt;?php\n\n//catch variable\n$givendate=$_GET[\'given\'];\n\n//convert given date to unix timestamp\n$date=strtotime($givendate);\necho "Date Set As...: ".date(\'m/d/Y\',$date)."&lt;br /&gt;";\n\n//use given date to show day before\n$yesterday=strtotime(\'-1 day\',$date);\necho "Day Before: ".date(\'m/d/Y\',$yesterday)."&lt;br /&gt;";\n\n//same for next day\n$tomorrow=strtotime(\'+1 day\',$date);\necho "Next Day: ".date(\'m/d/Y\',$tomorrow)."&lt;br /&gt;";\n$lastmonday=strtotime(\'last monday, 1 week ago\',$date);\necho "Last Moday: ".date(\'D m/d/Y\',$lastmonday)."&lt;br /&gt;";\n\n//form\necho "&lt;form method=\\"get\\" action=\\"{$_SERVER[\'PHP_SELF\']}\\"&gt;";\n\n//link to subtract a day\necho "&lt;a href=\\"newtimetravel.php?given=".date(\'m/d/Y\',$yesterday)."\\"&gt;&lt;&lt; &lt;/a&gt;";\n\n//show current day\necho "&lt;input type=\\"text\\" name=\\"given\\" value=\\"$givendate\\"&gt;";\n\n//link to add a day\necho "&lt;a href=\\"newtimetravel.php?given=".date(\'m/d/Y\',$tomorrow)."\\"&gt; &gt;&gt;&lt;/a&gt;&lt;br /&gt;";\n\n//submit manually entered day\necho "&lt;input type=\\"submit\\" name=\\"changetime\\" value=\\"Set Current Date\\"&gt;";\n\n//close form\necho "&lt;form&gt;&lt;br /&gt;";\n?&gt;\n</code></pre>\n\n<p>Clicking on the "&lt;&lt;" and ">>" advances and retreats the day in question</p>\n'}, {'answer_id': 113115, 'author': 'Issac Kelly', 'author_id': 144, 'author_profile': 'https://Stackoverflow.com/users/144', 'pm_score': 4, 'selected': True, 'text': '<p>Working from previous calls to the same script isn\'t really a good idea for this type of thing.</p>\n\n<p>What you want to do is always pass two values to your script, the date, and the movement. (the below example is simplified so that you only pass the date, and it will always add one day to it)</p>\n\n<p>Example</p>\n\n<p><a href="http://www.site.com/addOneDay.php?date=1999-12-31" rel="noreferrer">http://www.site.com/addOneDay.php?date=1999-12-31</a></p>\n\n<pre><code>&lt;?php\n   echo Date("Y-m-d",(strtoTime($_GET[date])+86400));\n?&gt;\n</code></pre>\n\n<p>Please note that you should check to make sure that isset($_GET[date]) before as well</p>\n\n<p>If you really want to work from previous calls to the same script, you\'re going to have to do it with sessions, so please specify if that is the case.</p>\n'}]
['https://Stackoverflow.com/questions/8685', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1149/']