jonny512379 Posted September 14, 2015 Share Posted September 14, 2015 Hello All First question post, other than the usual introduction posts. FYI: i am OKish with php, but have never had a need to do the below I basically need to get every date in a given month, including the day of the week (i.e monday, etc). Ideally so i am given an array of dates (i.e with something like "$dates_arr = $calendar_obj->getDatesForMonth($month = 9, $year = 2015);") This is to build a kind of mini calendar. i would have assumed there are some inbuilt php functions/classes for this, but am struggling to find them. Can anyone tell me if there is anything i should be looking at, before i try to build my own class / extend DateTime, etc (as i don't think i need to reinvent the wheel) I would be grateful if anyone could point me in the right direction Quote Link to comment Share on other sites More sharing options...
Barand Posted September 14, 2015 Share Posted September 14, 2015 Have a look at the DateTime, DateInterval and DatePeriod classes DateTime DateInterval DatePeriod Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 14, 2015 Share Posted September 14, 2015 There is no built-in function/methods that I am aware of. But, the process of getting the data would be a very simple process. function getDOWs($year, $month) { $monthDays = array(); //Output variable $daysInMonthy = date('t', strtotime("{$year}-{$month}-01")); //# days in selected month for($day=1; $day<=$daysInMonthy; $day++) { //Create timestamp for current day of month $date = strtotime("{$year}-{$month}-{$day}"); //Add numeric day of month and the doy of week name to output array $monthDays[$day] = date('l', $date); } //Return results return $monthDays; } echo "<pre>"; print_r(getDOWs(2015, 9)); echo "<pre>"; Output for 2015 and 10 Array ( [1] => Tuesday [2] => Wednesday [3] => Thursday [4] => Friday [5] => Saturday [6] => Sunday [7] => Monday [8] => Tuesday [9] => Wednesday [10] => Thursday [11] => Friday [12] => Saturday [13] => Sunday [14] => Monday [15] => Tuesday [16] => Wednesday [17] => Thursday [18] => Friday [19] => Saturday [20] => Sunday [21] => Monday [22] => Tuesday [23] => Wednesday [24] => Thursday [25] => Friday [26] => Saturday [27] => Sunday [28] => Monday [29] => Tuesday [30] => Wednesday ) Quote Link to comment Share on other sites More sharing options...
jonny512379 Posted September 14, 2015 Author Share Posted September 14, 2015 Have a look at the DateTime, DateInterval and DatePeriod classes DateTime DateInterval DatePeriod Thank you! Quote Link to comment Share on other sites More sharing options...
jonny512379 Posted September 14, 2015 Author Share Posted September 14, 2015 There is no built-in function/methods that I am aware of. But, the process of getting the data would be a very simple process. function getDOWs($year, $month) { $monthDays = array(); //Output variable $daysInMonthy = date('t', strtotime("{$year}-{$month}-01")); //# days in selected month for($day=1; $day<=$daysInMonthy; $day++) { //Create timestamp for current day of month $date = strtotime("{$year}-{$month}-{$day}"); //Add numeric day of month and the doy of week name to output array $monthDays[$day] = date('l', $date); } //Return results return $monthDays; } echo "<pre>"; print_r(getDOWs(2015, 9)); echo "<pre>"; Output for 2015 and 10 Array ( [1] => Tuesday [2] => Wednesday [3] => Thursday [4] => Friday [5] => Saturday [6] => Sunday [7] => Monday [8] => Tuesday [9] => Wednesday [10] => Thursday [11] => Friday [12] => Saturday [13] => Sunday [14] => Monday [15] => Tuesday [16] => Wednesday [17] => Thursday [18] => Friday [19] => Saturday [20] => Sunday [21] => Monday [22] => Tuesday [23] => Wednesday [24] => Thursday [25] => Friday [26] => Saturday [27] => Sunday [28] => Monday [29] => Tuesday [30] => Wednesday ) Thank you very much, i will try to build a class, starting with what you have explained. Thanks! Quote Link to comment Share on other sites More sharing options...
Solution Barand Posted September 14, 2015 Solution Share Posted September 14, 2015 As Psycho's but using DateTime objects function getDatesForMonth($month, $year) { $int = new DateInterval('P1D'); // 1 day interval $dt1 = new DateTime("$year-$month-01"); $dt2 = (new DateTime($dt1->format('Y-m-t'))); // last day of month $dateper = new DatePeriod($dt1, $int, $dt2->add($int)); // create period from dt1 to dt2+1 $res = array(); foreach ($dateper as $d) { $res[$d->format('j')] = $d->format('D'); } return $res; } $dates = getDatesForMonth(9,2015); echo '<pre>',print_r($dates, true),'</pre>'; Quote Link to comment Share on other sites More sharing options...
jonny512379 Posted September 14, 2015 Author Share Posted September 14, 2015 just a quick question i notice in this post and in other post, variables are bing wrapped in {} eg: $daysInMonthy = date('t', strtotime("{$year}-{$month}-01")); //# days in selected month why is this? would this not be the same?: $daysInMonthy = date('t', strtotime("$year-$month-01")); //# days in selected month just asking out of curiosity Quote Link to comment Share on other sites More sharing options...
Barand Posted September 14, 2015 Share Posted September 14, 2015 In the case above it is optional but in other cases it is required, with array elements embedded in a string "Today is {$dates['dayOfWeek']}" if there is no space between the $var and the next character "Price : {$price}GBP" and the next character could be part of a legitimate variable name. Quote Link to comment Share on other sites More sharing options...
jonny512379 Posted September 14, 2015 Author Share Posted September 14, 2015 In the case above it is optional but in other cases it is required, with array elements embedded in a string "Today is {$dates['dayOfWeek']}" if there is no space between the $var and the next character "Price : {$price}GBP" and the next character could be part of a legitimate variable name. Thank you, makes perfect sense now, was just one of them things bugging me, and searching on google for "{} php" returns nothing (well it just ignores the {}) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.