Date() kalender

Discussion in 'Programming' started by Zynex, Feb 21, 2007.

  1. #1
    I am making a kalendar (date picker) and I was wondering how I can retrieve the number of days in the month. I can't go and declare the number of days for every month, thats too much code and not very effecient.

    thanx in advance,

    Zynex
     
    Zynex, Feb 21, 2007 IP
  2. Aragorn

    Aragorn Peon

    Messages:
    1,491
    Likes Received:
    72
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Which language are you using? PHP or Javascript?
    For PHP you can use
    
    function getDays($year, $month){
    	static $arMonths = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
    	// Reduce the month number by 1 to match our array index
    	$month--;
    	//If the month is february and the year is leap return 29 else, return the corresponding element from the array
    	return (((Year % 4 == 0 && Year % 100 != 0) || (Year % 400 == 0)) && $month == 1) 29 : $arMonths[$month];
    }
    
    PHP:
     
    Aragorn, Feb 21, 2007 IP
  3. ThomasNederman

    ThomasNederman Peon

    Messages:
    112
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #3
    The easy way is to create an array like
    $daysinmonth = array(31,28,31,30,31 etc)

    you can then display it with

    Echo "Jan has ".$daysinmonth[0]." days";
    (it is always month number -1 since a array starts at 0). I know some years it could be a problem with feb, but ce la vie, this is the easy solution.

    you can always make more complicated solutions, for exampel ms sql dateadd and add a month and take then a datediff in days etc, but let's not complicate things

    good luck
     
    ThomasNederman, Feb 21, 2007 IP
  4. picouli

    picouli Peon

    Messages:
    760
    Likes Received:
    89
    Best Answers:
    0
    Trophy Points:
    0
    #4
    In PHP you can date("t") to get the number of days in the given month:
    http://php.net/date

    HTH, cheers! :)
     
    picouli, Feb 21, 2007 IP