Re: Determining number of days in a selected month

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Apart from using date("t", $month) you can also determine the last 
day of a month by getting the 0th day of the following month.

Say I wanted to know the last day in Feb 2006:
$mnth = 2;
echo date("d",mktime(13,0,0,$mnth + 1, 0, 2006));

This is sometimes useful when you are calculating, or your code 
needs to walk up or down dates.

P.S. You may wonder why I specify 1pm as the time on the given day. 
If I don't (if I use 0) this is the same as saying midnight, in 
which case it is right at the very end of the day. This way I avoid 
daylight saving and/or minor hour/min/sec differences that might 
push the timestamp over into the next day. A small thing, but since 
I deploy code in a number of places, it has saved me countless 
headaches.

Geoff.


On 20 Jan 2006 at 13:51, Jeffrey Pearson wrote:

> The java mailing list I belong to recently had this same  
> conversation. I didn't know php had that ability built in. Java  
> doesn't (score another one for php). The cleanest solution I saw on  
> the Java list that would be non-language specific (in other words,  
> the same logic can be implemented in ANY language) would be to set a  
> variable to the first day of the month AHEAD of the month you want.  
> For example, if your looking for February, 2000, set the variable to  
> be March 1st, 2000. Then subtract 1 day. I have actually archived  
> this solution in my own little knowledge base as I thought it was  
> quite clever.
> 
> 
> Hope this helps other people as well.
> 
> 
> Jeff Pearson
> 
> 
> 
> 
> 
> 
> 
> 
> On Jan 20, 2006, at 8:17 AM, <tg-php@xxxxxxxxxxxxxxxxxxxxxx> <tg- 
> php@xxxxxxxxxxxxxxxxxxxxxx> wrote:
> 
> > date("t") will give you the number of days in the current month.
> >
> > Or you can do:
> >
> > date("t", mktime(0, 0, 0, $month, $day, $year))
> >
> >
> > to get the number of days in a specific month in a specific year  
> > (that way you can get leap year accurate counts..)
> >
> > -TG
> >
> > = = = Original message = = =
> >
> > Is there a way to easily tell PHP how many days there are in a  
> > selected
> > month without writing the following out for each month:
> >
> > if($Month == "January")
> > ~$NumberOfDays = 31;
> >
> > elseif($Month == "February")
> > ~if($Year == "A Leap Year")
> > ~~$NumberOfDays = 29;
> > ~
> > ~elseif($Year != "A Leap Year")
> > ~~$NumberOfDays = 28;
> > ~
> >
> > elseif($Month == "March")
> > ~$NumberOfDays = 31;
> >
> > ............and so on, and so on, and so on
> >
> > I would greatly appreciate any help.  Thanks.
> >
> >
> >
> > -- 
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> >
> > ___________________________________________________________
> > Sent by ePrompter, the premier email notification software.
> > Free download at http://www.ePrompter.com.
> >
> > -- 
> > PHP General Mailing List (http://www.php.net/)
> > To unsubscribe, visit: http://www.php.net/unsub.php
> >
> 
> -- 
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
> 
> 
> 
> !DSPAM:43d1ab1b244201588548228!
> 

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux