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
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
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);
SELECT `whatever`
FROM `article`
WHERE `publish_date` >= '2009-06-16'
AND `publish_date` <= '2009-06-23'
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.
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;
?>