Jump to content

Recommended Posts

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
?>

Link to comment
https://forums.phpfreaks.com/topic/83452-php-date-1/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/83452-php-date-1/#findComment-424660
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.