Jump to content

php date - 1 ?


jaguarr

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.