Jump to content

Minus 7 Days From a Date


Bopo

Recommended Posts

Hi

 

Well basically I'm going to use a between clause in a query, to return records between two dates, todays date, and the date from 7 days ago, I can easily get today's date via:

 

$todaysdate = date('Y-m-d');

 

But how would I get the 7 days ago date? I'm presuming I would have to do something with $todaysdate and store the output in a variable.

 

Help appreciated.

 

 

Link to comment
Share on other sites

Thanks for the suggestion, I have managed to output the current date minus 7 days, but when trying the query out, nothing is being returned and I'm not getting any errors from PHP

 

 

<?php 

if($getdate == "7day") {
$sevendays = date ( $format, strtotime ( '-7 day' . $date ) );
$sql = "SELECT * FROM comments WHERE date BETWEEN '$todaysdate' AND '$sevendays'";


}

include("blogconnect.php");

$query = mysql_query($sql, $connect) or die (mysql_error()); 
while ($row = mysql_fetch_assoc($query)) {   //this is where the error is highlighted

echo $row['id'] . '<br /><br />';
echo $row['comments'] . '<br /><br />';


?>

 

I have a hunch feeling that maybe the dates are not being treated as actual dates, but this is only a guess, and I don't know how to figure out if there not.

Link to comment
Share on other sites

Thanks for the suggestion, sadly I don't get any errors, here's all my current PHP, as maybe something else is causing it to fail.

 

<?php
ini_set('error_reporting', E_ALL);

session_start();

if($_SESSION['loggedin'] == "correct") {
echo "welcome";
} else {
header('Location: http://www.website.com/admin/login.php');
}

if(isset($_POST['submit'])) {

$getdate = $_POST['days'];
$todaysdate = date('Y-m-d'); 

$format = 'Y-m-d';
$date = date ( $todaysdate );


if($getdate == "7day") {
$sevendays = date ( $format, strtotime ( '-1 month' . $date ) );
$sql = "SELECT * FROM comments WHERE date BETWEEN '$todaysdate' AND '$sevendays'";


}

include("blogconnect.php");

$query = mysql_query($sql, $connect) or die (mysql_error()); 
while ($row = mysql_fetch_assoc($query)) {   //this is where the error is highlighted

echo $row['id'] . '<br /><br />';
echo $row['comments'] . '<br /><br />';

}
}
?>

 

SELECT * FROM comments WHERE date BETWEEN '2009-04-09' AND '2009-04-02'

Link to comment
Share on other sites

Thanks for that, sadly it's still not working, I quickly did a SELECT * FROM comments, and all the rows were returned, therefore it's not a connection error or anything, also my dates are being stored in the DATE format, and are stored as YYYY-DD-MM

Link to comment
Share on other sites

my dates are being stored in the DATE format, and are stored as YYYY-DD-MM

 

maybe something else is causing it to fail.

 

$format = 'Y-m-d';
$date = date ( $todaysdate );


if($getdate == "7day") {
$sevendays = date ( $format, strtotime ( '-1 month' . $date ) );

 

SELECT * FROM comments WHERE date BETWEEN '2009-04-09' AND '2009-04-02'

 

YYYY-DD-MM, eh

its seems as though you are formatting it as

YYYY-MM-DD

 

does this clear things up

Link to comment
Share on other sites

maybe try switching them around...i dunno, im not the greatest at querying dates...might wanna post this in the mysql board...the guys in there have helped me with NUMEROUS date queries similar to this.

 

$sql = "SELECT * FROM comments
    WHERE date BETWEEN DATE(DATE_SUBTRACT(CURDATE(), INTERVAL 7 DAY)) AND curdate() ";

Link to comment
Share on other sites

Okay either I'm a retard (probably) or MySQL hates me, but I'm getting another error with the query above

 

$sql = "SELECT * FROM comments WHERE date BETWEEN CURDATE() AND CURDATE() - INTERVAL 7 DAYS";

 

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'DAYS' at line 1

Link to comment
Share on other sites

take the S off of DAYS

 

MySQL should really fix a bug like that

 

The INTERVAL keyword and the unit specifier are not case sensitive.

 

The following table shows the expected form of the expr argument for each unit value.

unit Value Expected expr Format

MICROSECOND MICROSECONDS

SECOND SECONDS

MINUTE MINUTES

HOUR HOURS

DAY DAYS

WEEK WEEKS

MONTH MONTHS

QUARTER QUARTERS

YEAR YEARS

SECOND_MICROSECOND 'SECONDS.MICROSECONDS'

MINUTE_MICROSECOND 'MINUTES:SECONDS.MICROSECONDS'

MINUTE_SECOND 'MINUTES:SECONDS'

HOUR_MICROSECOND 'HOURS:MINUTES:SECONDS.MICROSECONDS'

HOUR_SECOND 'HOURS:MINUTES:SECONDS'

HOUR_MINUTE 'HOURS:MINUTES'

DAY_MICROSECOND 'DAYS HOURS:MINUTES:SECONDS.MICROSECONDS'

DAY_SECOND 'DAYS HOURS:MINUTES:SECONDS'

DAY_MINUTE 'DAYS HOURS:MINUTES'

DAY_HOUR 'DAYS HOURS'

YEAR_MONTH 'YEARS-MONTHS'

 

The return value depends on the arguments:

 

apparently it goes for everything

Link to comment
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.