Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Sep 2010
    Location
    Beaverton, OR
    Posts
    38
    Thanks
    3
    Thanked 2 Times in 2 Posts

    Need to find out if it's the second monday in the month, fourth tuesday, etc.

    Hi,

    So basically, I need to find out which day of the week it is, and then how many times that weekday has appeared this month (is it the first thursday, second monday, etc.).

    First, I'll get a date:

    PHP Code:
    $getdate date("Y\-m\-d"); 
    Next I need to find out what day of the week it is:

    PHP Code:
    $date_day date('N'strtotime($getdate)); 
    Next, and the part I'm struggling with, I need to find out which instance this day is in the month. Is it the first thursday? Second monday?

    I thought I'd found something that worked, but it just gave me what week it was, which is different. Like for today, it would give me the fifth week, but it's only the fourth wednesday.

    I'd really appreciate some help here. I keep finding ways to find out which week it is, but I can't find anything on which which instance a specific day of the week it is.

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    As far as I can see, this is a multistep process, as the logic is a bit more complex.

    First, you need to know how many days in the current month:

    PHP Code:
    $daysInMonth date('t'); 
    Then you need to figure out what day of the week the first day of the month is:

    PHP Code:
    $firstDay date('N'strtotime(date('Y-m-1'))); 
    And with that, you can figure out how many days of a particular type, by shifting by the offset of the first day:

    PHP Code:
    $dayCount $daysInMonth 7;
    $remainder $daysInMonth 7;
    if (
    $remainder >= date('N'strtotime($getdate)) - $firstDay$dayCount += 1
    Of course, there may be an easier way to do this, and I didn't run this code, so corrections are probably necessary. But the logic should be sound, I've done it before but it was a while ago.

  • #3
    New Coder
    Join Date
    Sep 2010
    Location
    Beaverton, OR
    Posts
    38
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Thanks for the help. I tried it out, but it seemed to just tell me how many times that day occurs. I was looking for something that could tell me not the total, but what number we're currently on (or whatever day we're examining is on). However, part of your code gave me an idea for a simple tweak to what I had before and I think I have it working now.

    The first day bit....I don't know why that didn't occur to me earlier, but it didn't. Anyway, remember I was saying earlier how I had a thing that would tell me which instance with regard to weeks? Well, I thought if I combined that with checking if the date day was less than the first day, then I could just subtract one from the week thing. It seems to work.

    The whole thing, if you're interested:
    PHP Code:
    function getWeeks($date)
    {
        
    $cut substr($date08);
        
    $daylen 86400;

        
    $timestamp strtotime($date);
        
    $first strtotime($cut "00");
        
    $elapsed = ($timestamp $first) / $daylen;

        
    $i 1;
        
    $weeks 1;

        for(
    $i$i<=$elapsed$i++)
        {
            
    $dayfind $cut . (strlen($i) < '0' $i $i);
            
    $daytimestamp strtotime($dayfind);

            
    $day strtolower(date("l"$daytimestamp));

            if(
    $day == strtolower("monday"))  $weeks ++;
        }

        return 
    $weeks;
    }

    $getdate date("Y\-m\-d");

    $first_day date('N'strtotime(date('Y-m-1'strtotime($getdate)))); 
    $date_day date('N'strtotime($getdate));
    $date_week getWeeks($getdate);

    if (
    $date_day $first_day) {
        
    $date_week $date_week 1;


  • #4
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Ah, I misunderstood your request. Yah, that works.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Perhaps I'm just not understanding what you are looking for, but this sounds to be as simple as:
    PHP Code:
    function weekOfMonth(DateTime $dt)
    {
        static 
    $aNth = array("first""second""third""fourth""fifth");
        return 
    $aNth[ceil($dt->format('d') / 7) - 1];

    That should tell you which week of the month it is with the string result. For a quick test, you can run with this:
    PHP Code:
    $dtStart = new DateTime();
    $dtEnd = clone $dtStart;
    $dtEnd->modify('+1 month');
    $diInterval = new DateInterval('P1D');
    $itor = new DatePeriod($dtStart$diInterval$dtEnd);
    foreach (
    $itor AS $dt)
    {
        
    printf('%s is the %s %s of %s' PHP_EOL,
            
    $dt->format('F j Y'),
            
    weekOfMonth($dt),
            
    $dt->format('l'),
            
    $dt->format('F'));

    And you can put whatever start date into the $dtStart to verify.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •