1// For given month
2echo cal_days_in_month(CAL_GREGORIAN, 1, 2021);
3// For current month
4echo date('t');
1echo Date("Y-m-d", strtotime("2013-01-01 +1 Month -1 Day"));// 2013-01-31
2
3echo Date("Y-m-d", strtotime("2013-01-31 +1 Month -3 Day")); // 2013-02-28
4
5echo Date("Y-m-d", strtotime("2013-01-31 +2 Month")); // 2013-03-31
6
7echo Date("Y-m-d", strtotime("2013-01-31 +3 Month -1 Day")); // 2013-04-30
8
9echo Date("Y-m-d", strtotime("2013-12-31 -1 Month -1 Day")); // 2013-11-30
10
11echo Date("Y-m-d", strtotime("2013-12-31 -2 Month")); // 2013-10-31
12
13echo Date("Y-m-d", strtotime("2013-12-31 -3 Month")); // 2013-10-01
14
15echo Date("Y-m-d", strtotime("2013-12-31 -3 Month -1 Day")); // 2013-09-30
1// get day of month php
2// Method 1; some server not work, I had check php 7.3.24 not worked, php 7.3.8 worked
3cal_days_in_month(CAL_GREGORIAN, $month, $year)
4echo (cal_days_in_month(CAL_GREGORIAN, 2, 2020)); // => 29
5
6// Method 2;
7function days_in_month($month, $year) {
8 // calculate number of days in a month
9 return $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year % 400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31);
10}
11
12// Method 3;
13echo (date('t', strtotime('2020-02-1'))); // 29
14
15
16