getting # of days in a month

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

getting # of days in a month

Post by psychotomus »

how can I get the # of days in a month and what day the 1st of the month starts on?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

given a timestamp within the month in question:

Code: Select all

$time = mktime(12,34,56,7,8,2009);
$daysInMonth = date('t', $time);
$firstIsA = date('l',mktime(6,0,0,date('j',$time),1,date('Y',$time));
Stewsburntmonkey
Forum Commoner
Posts: 44
Joined: Wed Aug 24, 2005 2:09 pm

Post by Stewsburntmonkey »

The last day of the month is the 0th day of the next month, so you can do this to print the number of days in a month:

Code: Select all

$lastday = mktime(0, 0, 0, 3, 0, 2000);
echo strftime("The number of days in Febuary in 2000 is: %d", $lastday);


To get the day of the week that a given date falls on use:

Code: Select all

date("l", mktime(0, 0, 0, 7, 1, 2000));


The above tells you what weekday July, 1st 2000 falls on. Just change the last 3 arguments to mktime() to suite your particular situation. :)
psychotomus
Forum Contributor
Posts: 487
Joined: Fri Jul 11, 2003 1:59 am

Post by psychotomus »

thanks
Post Reply