Jump to content


Getting info from a database by week...

  • Please log in to reply
1 reply to this topic

#1 djanim8

  • Members
  • PipPipPip
  • Advanced Member
  • 42 posts

Posted 23 August 2006 - 08:28 PM

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 :(

#2 Barand

  • Moderators
  • Sen . ( ile || sei )
  • 18,016 posts

Posted 23 August 2006 - 08:38 PM

$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;

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received


|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users