[thelist] PHP Dates Dilemma

rudy r937 at interlog.com
Wed Apr 17 16:26:01 CDT 2002


> What I would like to do is pragmatically figure out the dates
> that correspond to each Tuesday and Thursday of the month

hi burhan

could you be a bit more specific?  presumably you want only the remaining
tuesdays and thursdays of the current month, since tuesdays and thursdays
that have already gone by would not be good to sign up for, right?  and
what about if today is the 31st, don't you want a few tuesdays and
thursdays of the following month?

anyhow, here's how i would approach it

pick a convenient tuesday or thursday in the past, e.g. april 16, 2002

run the mysql TO_DAYS function on it, to determine what its day number is
(let's call this value "tue")

now, all tuesdays and thursdays after this date will have a day number that
has a difference of 0 or 2 when you apply modulus arithmetic

mysql has the convenient MOD(n,m) function which will come in handy here --
we will use WHERE MOD(something,7) IN (0,2)

all that remains is to generate future dates from today

for this purpose, my handy-dandy integers(tm) table will once again prove
helpful

 create table integers
    (i tinyint not null);
 insert into integers (i) values(1);
 insert into integers (i) values(2);
 insert into integers (i) values(3);
 insert into integers (i) values(4);
 insert into integers (i) values(5);
   etc.

now it's time to generate some dates

   select from_days(to_days(current_date) + i) as tuethurdate
       from integers
   where mod(to_days(current_date) + i - tue, 7) in (0,2)

in the above, substitute the previously-determined tuesday day number for
tue

the result will be a series of dates, only tuesdays and thursdays, for as
many days in advance of today as you have entries in the integers table

no need to mess with month boundaries, either


caution: not tested  (i usually make at least one mistake in the first
draft of any query i write)

rudy
http://rudy.ca/




More information about the thelist mailing list