views:

96

answers:

4
+1  Q: 

PHP Time and Date

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

+3  A: 

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);
rikh
I'm using MySQL
A Hassan
well, the query above will work great in MySQL.
rikh
yep. works. thank you very much!
A Hassan
A: 
SELECT `whatever`
FROM `article`
WHERE `publish_date` >= '2009-06-16'
AND `publish_date` <= '2009-06-23'
chaos
A: 

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.

mgroves
+1  A: 

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;
?>
Anders S