How to calculate the number of days in each month in php

you need to calculate the number of days from the current date to the 27th day of each month in PHP. In the code below, it correctly calculates the current month, but if the current date is 28, it should be calculated for the next month.

$year = date("y"); $month = date("m"); $day = '27'; $current_date = new DateTime(date('Ym-d'), new DateTimeZone('Asia/Dhaka')); $end_date = new DateTime("$year-$month-$day", new DateTimeZone('Asia/Dhaka')); $interval = $current_date->diff($end_date); echo $interval->format('%a day(s)'); 
+5
source share
5 answers

I wrote this script fast because I don’t have time to test it.

EDIT:

 $day = 27; $today = date('d'); if($today < $day){ $math = $day - $today; echo "There are " . $math . " days left until the 27th."; } else { $diff = date('t') - $today; $math = $diff + $day; echo "There are " . $math . " days left until the 27th of the next month."; } 
+2
source

Try the php function cal_days_in_month

 cal_days_in_month β€” Return the number of days in a month for a given year and calendar 

Example:

 $number = cal_days_in_month(CAL_GREGORIAN, 8, 2003); // 31 echo "There were {$number} days in August 2003"; 

Link

+5
source

Try entering the code,

 <?php $year = date("y"); $month = date("m"); $day = '27'; $current_date = new DateTime(date('Ym-d'), new DateTimeZone('Asia/Dhaka')); $end_date = new DateTime("$year-$month-$day", new DateTimeZone('Asia/Dhaka')); if($current_date->getTimestamp()<=$end_date->getTimestamp()){ $interval = $current_date->diff($end_date); echo $interval->format('%a day(s)'); } else{ $interval = $end_date->diff($current_date); echo $interval->format('-%a day(s)'); } ?> 
+1
source
 $now = time(); // or your date as well $your_date = strtotime("2010-01-01"); $datediff = $now - $your_date; echo floor($datediff / (60 * 60 * 24)); 

Source: Find the number of days between two dates.

0
source

this ....

 <?php $d=cal_days_in_month(CAL_GREGORIAN,10,2005); echo "There was $d days in October 2005"; ?> 
-1
source

Source: https://habr.com/ru/post/1269096/


All Articles