Going through relative dates using strtotime ()

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

Example:

  • This is the 10th day of the month, I press the "-1 day" button, and now the date reads like the 9th of the month.
  • I press the β€œ-1 day” button again, and now the readings show the 8th day.
  • I press the +1 day button, and now the readings show the 9th.

I understand buttons and displaying dates and using $_GET and PHP to convey information, but how do I get strtotime() to work with a relative date from the last strtotime() time travel script?

My work allowed me to show yesterday and today regarding the present, but not relative, for example, the day before yesterday or the day after tomorrow. Or if I use my last Monday button, the day before or after that day.

+6
date php strtotime
source share
3 answers

Working with previous calls with the same script is actually not a good idea for this type of thing.

What you want to do always passes two values ​​to your script, date and movement. (the example below is simplified, so 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("Ymd",(strtoTime($_GET[date])+86400)); ?> 

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

If you really want to work with previous calls with the same script, you will have to do this with sessions, so please indicate if this is the case.

+6
source share

Kevin, you are working with an absolute absolute base (i.e. date / time), not a relative time period. Then you convert to relative time periods. So, for example, by default, if you display a calendar, you will work from today’s date.

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

You can see strtotime in the function definition here, the second argument now, i.e. You can change the date from which it is relative.

It might be easier to display through a quick loop.

This will go through the last 10 days, using "yesterday" as the first argument. Then we use the date to print.

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

So pass the time / date through the URI to keep the relative date.

+1
source share

After a moment of inspiration, the solution to my question became obvious (I was riding my bike). Part "$ now"

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

must be set as the current date. Not "$ time () - now", but "the current date I'm associated with // I'm looking at my journal.

ie: if I look at the schedule summary for 8/10/2008, then it is "now" according to strtotime (); Yesterday - 8/09, and tomorrow - 8/11. Once I sneak up once, "now" - 8/11, yesterday - 8/10, and tomorrow - 8/12.

Here is a sample code:

 <?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 />"; ?> 

By clicking on the button "<lt; and" β†’ "advances and retreats that day.

0
source share

All Articles