Jump to content


This topic is now archived and is closed to further replies.


Getting info from a database by week...

Recommended Posts

I want to get all records from a mySQL database that have a date for this week (starting on Sunday)..

I can get the day of the week (today is Wed...) by doing this: date("w");

so how can I subtract this many days (3) from today's date to get the start? and then add 6 to that date to get the end.. for example:

this is todays date: 08/23/2006

I want to make a dymanic sql statement like this for THIS week (and then whatever week after that).

$mySQL = "SELECT * FROM thistable WHERE thisdate IS BETWEEN ".$firstDate." AND ".$lastDate;

Where first date would be this sunday (08/20/2006) and the last date would be this saturday (08/26/2006)

I can't seem to find code to add and subtract from the current date :(

Share this post

Link to post
Share on other sites
$d = "08/23/2006";
$t = strtotime($d);
$dow = date('w', $t);
$d1 = strtotime ("-$dow days", $t);
$d2 = strtotime ("+6 days", $d1);

$firstdate = date ('Y-m-d', $d1);
$lastdate = date ('Y-m-d', $d2);

echo $firstdate, ' ', $lastdate;

Share this post

Link to post
Share on other sites


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.