PHP: cal_days_in_month - Manual

downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

cal_from_jd Calendar

Last updated: Sat, 27 Jan 2007

view this page in

# cal_days_in_month

(PHP 4 >= 4.1.0, PHP 5)

cal_days_in_month -- Return the number of days in a month for a given year and calendar

## Description

int cal_days_in_month ( int calendar, int month, int year )

This function will return the number of days in the `month` of `year` for the specified `calendar`.

Пример 1. cal_days_in_month() example

`<?php $num = cal_days_in_month(CAL_GREGORIAN, 8, 2003); // 31 echo "There was $num days in August 2003"; ?>`

See also jdtounix().

add a note User Contributed Notes
cal_days_in_month
sasha at alronix dot com
05-Apr-2006 06:46
`function days_in_month($a_month, $a_year) {
return date('t', strtotime($a_year . '-' . $a_month . '-01'));
}`
NiGhMa
23-May-2005 01:00
`Stay simple ;)
$ndays = date("t"); //return the number of days for this month and this year
$ndays = date("t", mktime(0, 0, 0, 12, 1, 2004)); // You can also specify a date
c u`
Pasci - webmaster at cyber-style dot net
07-Jan-2005 02:21
`Here's the one line solution without loops or other slow stuff ;)
<?php
$daynum = date("j",mktime(0,0,0,$month+1,0,$year));
?>`
richard at N O S P A M dot computoz dot co dot uk
24-Aug-2004 05:55
`re: rshowalter at mtsinai dot on dot ca 's daysinmonth.
This is faster (no loop, no testing irrelevant values):
function daysinmonth($month, $year)
{
if(checkdate($month, 31, $year)) return 31;
if(checkdate($month, 30, $year)) return 30;
if(checkdate($month, 29, $year)) return 29;
if(checkdate($month, 28, $year)) return 28;
return 0; // error
}
and won't inf-loop on you, ever.`
rshowalter at mtsinai dot on dot ca
27-Apr-2004 05:43
`How about this one:
<?php
function daysinmonth($month, $year)
{
$days=31;
while(!checkdate($month, $days, $year)) $days--;
return $days;
}
?>
Not exactly a one-line solution, but easy to follow, not limited to 1970-2038 (but don't use it after the year 32767) and uses a built-in function that's always available.
Be sure that $month and $year are valid, or the loop will run forever. Possibly add:
&& $days>0
to the while condition. This would return 0 as your error code.`
paul (at) bughunt3r.com
18-Apr-2004 08:31
`Here is a nifty function to return the date for easter for ranges beyond 2037 or before 1970.
<?php
function str_easter_date ($year) {
$days = easter_days ($year);
$days_in_march = cal_days_in_month (CAL_GREGORIAN, 3, $year);
if (($days + 21) < $days_in_march) {
$month = "March";
$date = $days + 21;
} else {
$month = "April";
$date = ($days + 21) - $days_in_march;
}
return "$month $date, $year";
}
?>`
jeffbeall at comcast dot net
14-Apr-2004 10:04
`This will work great in future dates but will give the wrong answer for dates before 1550 (approx) when leap year was introduced and the calendar lost a year or two.
Sorry now to be more specific it has been a while sine I had to account for those later dates and had to take that into account but just a heads up for others to watch out.`
dbindel at austin dot rr dot com
01-Jan-2004 09:06
`Here's a one-line function I just wrote to find the numbers of days in a month that doesn't depend on any other functions.
The reason I made this is because I just found out I forgot to compile PHP with support for calendars, and a class I'm writing for my website's open source section was broken. So rather than recompiling PHP (which I will get around to tomorrow I guess), I just wrote this function which should work just as well, and will always work without the requirement of PHP's calendar extension or any other PHP functions for that matter.
I learned the days of the month using the old knuckle & inbetween knuckle method, so that should explain the mod 7 part. :)
<?php
/*
* days_in_month($month, $year)
* Returns the number of days in a given month and year, taking into account leap years.
*
* $month: numeric month (integers 1-12)
* $year: numeric year (any integer)
*
* Prec: $month is an integer between 1 and 12, inclusive, and $year is an integer.
* Post: none
*/
// corrected by ben at sparkyb dot net
function days_in_month($month, $year)
{
// calculate number of days in a month
return $month == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year % 400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31);
}
?>
Enjoy,
David Bindel`
add a note
cal_from_jd Calendar

Last updated: Sat, 27 Jan 2007
show source | credits | stats | sitemap | contact | advertising | mirror sites
Copyright © 2001-2007 The PHP Group
All rights reserved.
This mirror generously provided by: Yahoo! Inc.
Last updated: Sat Mar 10 11:33:19 2007 PST