Reputation: 219
I have a database with dated articles. What I want to do is select articles between 2 dates - for example from 7 days ago to today.
Can anybody help me. I have been trying to write a code for it but it hasn't worked for me.
Thanks in advance
Upvotes: 1
Views: 292
Reputation: 249
If you are using timestamps you can try something like this:
<?php
$toDate = time();
$fromDate = $now - (60 * 60 * 24 * 7);
$query = 'SELECT * FROM table WHERE time>='.$fromDate.' AND time<='.$toDate;
?>
Upvotes: 1
Reputation: 26096
SELECT *
FROM yourTable
WHERE articleDate >= '2009-05-01'
AND articleDate <= '2009-05-31'
I suspect you're having trouble formatting dates, so I'd suggest looking into the PHP date() and strtotime() functions.
Upvotes: 0
Reputation: 124267
SELECT `whatever`
FROM `article`
WHERE `publish_date` >= '2009-06-16'
AND `publish_date` <= '2009-06-23'
Upvotes: 0
Reputation: 13972
If your database is SQL based, try this...
SELECT * FROM articles WHERE published > DATE_SUB(NOW(), INTERVAL 7 DAY)
If you are working just in PHP, you can manipulate dates a bit like this...
$now = time();
// go back 7 days by working out how many seconds pass in 7 days
$lastweek = $now - (60*60*24*7);
// format the date from last week any way you like...
echo date("r", $lastweek);
Upvotes: 3