php date - find first monday in month

  • jokerper
  • Novice
  • Novice
  • User avatar
  • Posts: 22

Post 3+ Months Ago

hi,


how do I find this

php date - find first Monday in month.

like today 14.1.2012 I won to find the first Monday in February now and after this Monday I want to find the next in March and so on except of June.

any help, please?
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • WritingBadCode
  • Graduate
  • Graduate
  • User avatar
  • Posts: 214
  • Loc: Sweden

Post 3+ Months Ago

If I understand dates correctly you can assume that you will have a monday happening every 7 days. :D And you can also safley assume that you will only have one monday each 7 days.

Anyway here is some code that prints you if its monday for the first time this month:

Code: [ Select ]
if ( date("D") === "Mon" && (date("j")) < 8 )
{
    echo "Today is the the first Monday this month!";
}
  1. if ( date("D") === "Mon" && (date("j")) < 8 )
  2. {
  3.     echo "Today is the the first Monday this month!";
  4. }


The code above will only check todays day and if it its the first monday this month then it will trigger and say that today.. bla bla bla. You wasn't asking for that.

Anyway here is some code that I wrote in a hurry:

Code: [ Select ]

$day = 1;
$month = 1;
$yearNow = 2011; //start counting from this year
$countToYear = 2020; // stop when reaching this year
while ($yearNow < $countToYear)
{
    if ( $month == 6 ) //skip June (for some reason?)
    {
        $month++;
        continue;
    }
    if ( date("l", mktime(0, 0, 0, $month, $day, $yearNow)) == "Monday" ) // prints out IF monday
    {
        echo date("Y-m-d", mktime(0, 0, 0, $month, $day, $yearNow));
        echo "<br />";
    }
    $day++;
    if ($day > 7) /* there are more than 7 days in a month but the first monday will happen in the first 7 days, so start over. with new month please */
    {
        $day = 1;
        $month++;
        if($month > 12)
        {
            $month = 1;
            $yearNow++;
        }
    }
}
  1. $day = 1;
  2. $month = 1;
  3. $yearNow = 2011; //start counting from this year
  4. $countToYear = 2020; // stop when reaching this year
  5. while ($yearNow < $countToYear)
  6. {
  7.     if ( $month == 6 ) //skip June (for some reason?)
  8.     {
  9.         $month++;
  10.         continue;
  11.     }
  12.     if ( date("l", mktime(0, 0, 0, $month, $day, $yearNow)) == "Monday" ) // prints out IF monday
  13.     {
  14.         echo date("Y-m-d", mktime(0, 0, 0, $month, $day, $yearNow));
  15.         echo "<br />";
  16.     }
  17.     $day++;
  18.     if ($day > 7) /* there are more than 7 days in a month but the first monday will happen in the first 7 days, so start over. with new month please */
  19.     {
  20.         $day = 1;
  21.         $month++;
  22.         if($month > 12)
  23.         {
  24.             $month = 1;
  25.             $yearNow++;
  26.         }
  27.     }
  28. }


Its not pretty but it should give you a list looking like:

2011-01-03
2011-02-07
2011-03-07
2011-04-04
2011-05-02
2011-07-04
2011-08-01
2011-09-05
2011-10-03
2011-11-07
2011-12-05
2012-01-02
2012-02-06
2012-03-05
2012-04-02
2012-05-07
2012-07-02
2012-08-06
2012-09-03
2012-10-01
2012-11-05
2012-12-03
2013-01-07
2013-02-04
2013-03-04
2013-04-01
2013-05-06
etc

These are the first time a monday is happening in each month. As you can see 20XX-06-XX is skipped since you didn't want to calculate when it happens on June. This is made by using a continue statment:

Code: [ Select ]
  if ( $month == 6 )
  {
    $month++;
    continue;
  }
  1.   if ( $month == 6 )
  2.   {
  3.     $month++;
  4.     continue;
  5.   }


You can check dates further in time or go back in time more by altering $yearNow and $countToYear's initial starting values. I would let the $day = 1; and $month = 1; values be as is, they are just telling it to start the first "scan" with month 1 and day 1.
  • jokerper
  • Novice
  • Novice
  • User avatar
  • Posts: 22

Post 3+ Months Ago

Hi,

First thanks for your reply.

I can see it work great.

Can you tell me where to change if I won the 2. or 3.
4. or 5.

because I will update some events in the future.

So I can use it to put it in a while and insert into a db.

all the scheudle(wrong) in a db.

Thanks in advance.

/jokerper

Post Information

  • Total Posts in this topic: 3 posts
  • Users browsing this forum: No registered users and 62 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.