Forum Moderators: coopster

Message Too Old, No Replies

Adding/Subtracting from Current Day's Date

         

dougmcc1

8:48 pm on Jun 1, 2006 (gmt 0)

10+ Year Member



Let's say today's date is 2006-06-01.

How would I get data for the previous week without having to specify it? For example, the following wouldn't work:

$enddate=date(Ymd);
$startdate=date(Ymd)-6;
SELECT data FROM table WHERE date BETWEEN '$startdate' AND '$enddate'

I want $startdate to be equal to 2006-05-26. Is this possible?

omoutop

6:09 am on Jun 2, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



$today = date("m-d-Y"); // 06-01-2006
$temp_today = mktime(0,0,0,date("m"),date("d"),date("Y"))

$temp_last_week = mktime(0,0,0,date("m"),date("d")-7,date("Y"))

OR

$temp_last_week = $temp_today - 604800 (which is 86400x7) (86400 is the seconds per day(24h))

$last_week = date("m-d-Y",$temp_last_week);

Hope this helps

dreamcatcher

7:13 am on Jun 2, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi dougmcc1,

This will also work:

$enddate = date("Y-m-d");
$startdate = date("Y-m-d", strtotime("-6 days"));

dc

dougmcc1

2:23 pm on Jun 2, 2006 (gmt 0)

10+ Year Member



Thanks!

coopster

2:54 pm on Jun 2, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



If you are merely building the SQL values you can let your database do all the work:
SELECT 
data
FROM table
WHERE date BETWEEN CURRENT_DATE - INTERVAL 6 DAY AND CURRENT_DATE
;

Note that if you truly are using

date
as a column name you may want to reconsider as it is a reserved word in most relational database management systems.