jaguarr Posted December 28, 2007 Share Posted December 28, 2007 I am learning php and mysql by making a online diary site. I can submit the diary to the database now and have it displayed. I want to add a function which the user can view the diary of the previous day. I see that you can use newDate = date('Y-m-d', strtotime("yesterday"));(, which works if the user clicks the link once. But what if the user wants to view the diary one more day before (the day before yesterday)? With the following test script, I try to assign the $newDate variable but it doesn't work. I searched online to see if you can use php to subtract one day, but with no luck. <?php include("config.php"); //database information $date = date("Y-m-d"); //gets the date today $newDate = date('Y-m-d', strtotime("yesterday")); //gets the date yesterday ?> <!-- using form to pass the date --> <form method="post" action="<?php echo $_SERVER['PHP_SELF']?>"> Date:<input type="Text" name="date" value="<?php echo $newDate ?>"> <input type="Submit" name="submit" value="Enter information"> </form> <?php $Date = $_REQUEST['date'] ; echo "Date: " . $Date . "<br><br>"; $result = mysql_query("select * from news where dtime = '$Date'", $connect); $myrow = mysql_fetch_array($result); echo $myrow['text1']; //display the diary $newDate = "2007-12-27"; //this doesn't work ?> Quote Link to comment https://forums.phpfreaks.com/topic/83452-php-date-1/ Share on other sites More sharing options...
corbin Posted December 28, 2007 Share Posted December 28, 2007 strtotime('-1 days'); strtotime('-2 days'); So on should do the trick.... You'll just need to keep track of the -x variable so that you'll know which one is next. Quote Link to comment https://forums.phpfreaks.com/topic/83452-php-date-1/#findComment-424563 Share on other sites More sharing options...
monkeytooth Posted December 28, 2007 Share Posted December 28, 2007 Insert taken from php.net.. <?php $nextWeek = time() + (7 * 24 * 60 * 60); // 7 days; 24 hours; 60 mins; 60secs echo 'Now: '. date('Y-m-d') ."\n"; echo 'Next Week: '. date('Y-m-d', $nextWeek) ."\n"; // or using strtotime(): echo 'Next Week: '. date('Y-m-d', strtotime('+1 week')) ."\n"; ?> outputs: Now: 2005-03-30 Next Week: 2005-04-06 Next Week: 2005-04-06 just a thought for another way to spin it. Cause if you set a unix timestamp with every entry.. and then make it do the calculation from what ever today is back.. your script can just say how many days years what ever back it spans to.. I don't know if I just made any sence in that last paragraph Im a bit sleepy but I hope the idea is kinda there Quote Link to comment https://forums.phpfreaks.com/topic/83452-php-date-1/#findComment-424660 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.