| 
	
 | 
 Posted by Chris Hope on 11/19/06 23:02 
Greg Scharlemann wrote: 
 
> I have a series of records in a database. When each record is stored, 
> the datetime is logged: $date = date("Y-m-d H:i:s"); 
>  
> Prior to adding a new record to the database, I want to run a query to 
> retrieve all of the records uploaded in the last 7 days.  I thought it 
> would be easiest to: 
>  
> Pseudo Code: 
> $newDate = $date - 7 days; 
> select * from TABLE where DATE > $newDate; 
>  
> Problem is I can't figure out how to subtract 7 days from $date and 
> convert that value to a valid datetime object. 
>  
> Thanks for any help 
 
You can do it in SQL. Assuming you're using MySQL look for SUBDATE() on 
the following page: 
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html 
 
If you want to do it in PHP you can do eg: 
date('Y-m-d H:i:s', time() - 86400 * 7); 
 
time() returns the current timestamp. There are 86400 seconds in a day 
so multiplying that by 7 gives you the seven days you're after. Then 
use date() to format and put into your sql statement. 
 
It's also possible to do it with mktime() eg: 
$timestamp = mktime(0, 0, 0, date('m'), date('d')-7, date('Y')) 
although there are more function calls doing it this way. 
See http://www.php.net/mktime for more details 
 
--  
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com
 
  
Navigation:
[Reply to this message] 
 |