Days in month?

Hello! Need help with this!
I have this code to show the days in every month.
And every month is ok, they start and stor on the right day.
EXCEPT for august!
Why???

This is the code:

$months = [ 1 => "Jan", 2 => "Feb", 3 => "Mars", 4 => "April",
		5 => "Maj", 6 => "Jun", 7 => "Juli", 8 => "August",
							9 => "Sept", 10 => "Oct", 11 => "Nov", 12 => "Dec"
						];
						$monthNow = date("m");
						echo "<select id='calmonth'>";
						foreach ($months as $m=>$mth) {
							printf("<option value='%s'%s>%s</option>", 
							$m, $m==$monthNow?" selected":"", $mth
							);
						}
						echo "</select>";?>

There’s nothing in the posted code having anything to do with the days in a month.

The only potential problem in the posted code is that date("m") includes a leading zero, which when treated as a number by php is an octal number, and 08 and 09 are not valid octal numbers. You would want to use date("n") to get just 1-12, without a leading zero.

Well, there are date and time functions for all that. No need to do all that code.
To get the number of days in the current month, use this:

  $days = date("t"); 

If you need to get days of another month, you can do it this way:

  $month = 8;
  $year = 2003;
  $days = cal_days_in_month(CAL_GREGORIAN, $month, $year);  

Now with that said, you might want to look into Jquery “Date-Pickers” since they are easy to use and look very nice on a webpage.

Oh, also, there are tons of date functions for getting the current month, year, getting the day of the week from a date, calculating the difference in days or weeks of two date and so many more. All are there to make things simpler in PHP. Hope this helped!

Sponsor our Newsletter | Privacy Policy | Terms of Service