#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2004
    Posts
    29
    Rep Power
    0

    first/second/third/etc. monday of each month?


    Trying to make a calendar type script where one can store events that will happen on alternate weeks or every 3 weeks or 4 weeks etc. on a given day

    say an event happened every 2nd monday of every month. Is there an easy way to use date() to find the second monday or would i need to iterate through the days anc check/count each?

    cheers
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Dallas, TX
    Posts
    944
    Rep Power
    11
    Do some reading on the PHP function strtotime() and mktime().

    Together they can calculate any date you need.

    strtotime() uses GNU getdate syntax, and can accept "relative" offets to dates.
    (like next monday, 3rd monday etc).

    This makes it very easy to calculate these types of dates without worrying about leap year, daylight savings, timezones etc.

    PHP Code:

    $tc 
    strtotime('1st monday'mktime(000date('m'), 1) );
    print 
    '<P/>The first monday of this month is ' date("m/d/Y"$tc);

    $tc strtotime('1st monday'mktime(000date('m')+11) );
    print 
    '<P/>The first monday of next month is ' date("m/d/Y"$tc);

    $tc strtotime('2nd monday'mktime(000date('m')+11) );
    print 
    '<P/>The second monday of next month is ' date("m/d/Y"$tc);

    $tc strtotime('3rd monday'mktime(000date('m')+11) );
    print 
    '<P/>The third monday of next month is ' date("m/d/Y"$tc); 
    Last edited by UnseenForces; December 24th, 2004 at 01:38 PM.
    Pay no attention to the person behind the curtain.

IMN logo majestic logo threadwatch logo seochat tools logo