2

My previous question: how-to-find-current-day-no-in-month-in-php

Now I face another problem. Suppose I know that this monday is the 4th monday of the current month. Now how to check that this monday is also the last monday of the current month?

exa:1 
date: 27-01-2014

Using the code below, I get the name and number of a day:

day: monday   
day no :4th

This is the 4th monday of january and it is also the last monday of january. How to check that?

exa:2  
date: 20-01-2014   
day: monday   
day no :3rd

Here this monday is 3rd monday of january but not last monday of january.

So, in short, when I got a day number then how to check whether that the day number is last or not?

code:

$t=date('d-m-Y');
$dayName = strtolower(date("D",strtotime($t)));
$dayNum = strtolower(date("d",strtotime($t)));
$dayno = floor(($dayNum - 1) / 7) + 1
Community
  • 1
  • 1
DS9
  • 2,995
  • 4
  • 52
  • 102

3 Answers3

6

The quickest way to check if today is the last of the month I would suggest the following

 if(date('t') == date('d')){
    echo 'Last day of the month.';
}

This compares the current day with the number of days of the current month.

Sebo
  • 443
  • 6
  • 14
gabisajr
  • 146
  • 1
  • 5
1

Just add 7 days (you still have a monday) to the date you have, and check if it's in the same month.

Nanne
  • 64,065
  • 16
  • 119
  • 163
0

The complete solution is as follow. Thanks to @Oliver M Grech and @Nanne.

            $t=date('d-m-Y');
            $day = strtolower(date("D",strtotime($t)));
            $month = strtolower(date("m",strtotime($t)));
            $dayNum = strtolower(date("d",strtotime($t)));
            $weekno = floor(($dayNum - 1) / 7) + 1;

            if($weekno=="4" or $weekno=="5")
            {
                $Date = date("d-m-Y");
                $new_month = date('m', strtotime($Date. ' + 7 days'));
                if($new_month != $month)
                {
                    echo "This ".$day." is last day of month." ;
                }
            }
DS9
  • 2,995
  • 4
  • 52
  • 102