Question

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

Example:

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

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

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

Was it helpful?

Solution

Working from previous calls to the same script isn't really a good idea for this type of thing.

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)

Example

http://www.site.com/addOneDay.php?date=1999-12-31

<?php
   echo Date("Y-m-d",(strtoTime($_GET[date])+86400));
?>

Please note that you should check to make sure that isset($_GET[date]) before as well

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.

OTHER TIPS

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.

int strtotime  ( string $time  [, int $now  ] )

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.

This might be easier to display through a quick loop

This will loop through the last 10 days using "yesterday" as the first argument. We then use date to print it out.

$time = time();

for ($i = 0; $i < 10; $i++) {
    $time = strtotime("yesterday", $time);
    print date("r", $time) . "\n";
}

So pass the time/date in via the URI so you can save the relative date.

After a moment of inspiration, the solution to my question became apparent to me (I was riding my bike). The '$now' part of

strtottime( string $time {,int $now ]) 

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.

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.

Here's the code example:

<?php

//catch variable
$givendate=$_GET['given'];

//convert given date to unix timestamp
$date=strtotime($givendate);
echo "Date Set As...: ".date('m/d/Y',$date)."<br />";

//use given date to show day before
$yesterday=strtotime('-1 day',$date);
echo "Day Before: ".date('m/d/Y',$yesterday)."<br />";

//same for next day
$tomorrow=strtotime('+1 day',$date);
echo "Next Day: ".date('m/d/Y',$tomorrow)."<br />";
$lastmonday=strtotime('last monday, 1 week ago',$date);
echo "Last Moday: ".date('D m/d/Y',$lastmonday)."<br />";

//form
echo "<form method=\"get\" action=\"{$_SERVER['PHP_SELF']}\">";

//link to subtract a day
echo "<a href=\"newtimetravel.php?given=".date('m/d/Y',$yesterday)."\"><< </a>";

//show current day
echo "<input type=\"text\" name=\"given\" value=\"$givendate\">";

//link to add a day
echo "<a href=\"newtimetravel.php?given=".date('m/d/Y',$tomorrow)."\"> >></a><br />";

//submit manually entered day
echo "<input type=\"submit\" name=\"changetime\" value=\"Set Current Date\">";

//close form
echo "<form><br />";
?>

Clicking on the "<<" and ">>" advances and retreats the day in question

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top