简体   繁体   中英

How to check that day is last day of month in php?

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

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.

只需将 7 天(您还有一个星期一)添加到您拥有的日期,并检查它是否在同一个月。

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." ;
                }
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM