>Today is Thursday, August 18, 2004. I would like to have a variable
In my world, August 18, 2004 is a WEDNESDAY.
that stores the date this coming Sunday (even if today happened to be
Sunday) in YYY-MM-DD format.
This format has a severe Y1K problem.
I also need a variable that will store
the date this past Monday (even if today happens to be Monday) also in
YYY-MM-DD format.
This format still has a severe Y1K problem.
So, the end result is that, for this week (today is 2004-08-18) I
would have the following two variables:
$this_sunday = 2004-08-21 // This coming Sunday
Um, the 21st is a SATURDAY.$this_monday = 2004-08-14 // This past Monday
Um, the 14th is a SATURDAY.
date('w') is the current weekday (0-6).
$x = (0 - date('w')) % 7;
is the number of days you need to add to the current date to
get to next Sunday. (Assuming that if today IS Sunday, you
want today). 0=Sunday If today is Thursday, that's (0-4)%7,
which is 3 days to add.
$x = (date('w') - 1) % 7;
is the number of days you need to subtract from the current
date to get to last Monday. (Assuming that if today IS Monday,
you want today.) 1=Monday If today is Thursday, that's (4-1)%7,
which is 3 days to subtract.
date('Y-m-d', time() + $x*24*60*60)
is the date $x days before (if $x is negative) or after
today in the format you want above. WARNING: there will
be glitches in this around daylight savings time transitions.
I think date('Y-m-d', mktime(0, 0, 0, date('m)', date('d')+$x,
date('Y'))) works better for this (UNIX mktime() allows you
to feed it things like March -1, 2004 and end up with
2004-02-29. I'm not sure that there can't be glitches in
this due to leap seconds.
Gordon L. Burditt