biggieuk Posted September 20, 2010 Share Posted September 20, 2010 Hi all, I've been trying to find a simple script that can output an array of months within a given year which each contain the days for that month. Something along the lines of this: [1] => Array ( [0] => Array ( [num] => 1 [ts] => 2011/08/01 ) .... 1 being the month (jan) 0 is the day (which contains the day & full date) Does anybody know of a script that does something along these lines as all the ones i have found seem over compliated and are outputting into tables whereas i only need this as an array? Thanks! Quote Link to comment Share on other sites More sharing options...
litebearer Posted September 20, 2010 Share Posted September 20, 2010 Have you looked at... http://www.micronetwork.de/activecalendar/ Quote Link to comment Share on other sites More sharing options...
Psycho Posted September 20, 2010 Share Posted September 20, 2010 Can you explain the purpose of this array? Creating an array of all the days in the years seems wholly unnecessay given all the avaialble data/time features built into PHP where you can get what you need dynamically. Quote Link to comment Share on other sites More sharing options...
biggieuk Posted September 20, 2010 Author Share Posted September 20, 2010 Hi, Thanks for your replies. I am using Smarty PHP engine to display a calendar from the current month up until the same month in the following year. I need to output this array in PHP so that i can use Smarty's templating scripts to output the data as i like. I have found i can loop through a range of dates using $curr_date = date("Y-m-d", strtotime("+1 month", strtotime($curr_date))); to add a month onto the date each iteration. I am unsure as to how I can find out what day the month starts on and output this into an array. I have attatched a sample of the mini calendar I am going for. [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
biggieuk Posted September 21, 2010 Author Share Posted September 21, 2010 It's ok, I did this myself eventually. // Calendar $cal = array(); $uid = 0; // Current date & end date $curr_date = date('Y-m-d'); $end_date = date("Y-m-d", strtotime($curr_date . "+12 month")); // Loop through year while (strtotime($curr_date) < strtotime($end_date)) { // Set variables for new date $curr_month_name = date('F', strtotime($curr_date)); $curr_month = date('n', strtotime($curr_date)); $curr_year = date('Y' , strtotime($curr_date)); $start_day = mktime(0, 0, 0, $curr_month, 1, $curr_year); $start_day_number = date ( 'w', $start_day ); $days_in_month = date ( 't', $start_day ); $trow = 0; $blank_days = 0; $blank_days = $start_day_number - 1; if ( $blank_days < 0 ) { $blank_days = 7 - abs ( $blank_days ); } // Prefix blank days for ( $x = 0 ; $x < $blank_days ; $x++ ) { $cal[ $curr_month ][ $trow ]['num'] = null; $trow++; } for ( $x = 1 ; $x <= $days_in_month ; $x++ ) { // Output ID $cal[ $curr_month ][ $trow ]['id'] = $uid; // Output Day $cal[ $curr_month ][ $trow ]['num'] = $x; // Output YYYY-MM-DD $cal[ $curr_month ][ $trow ]['ts'] = $curr_year."-".$curr_month."-".sprintf('%02d', $x); $uid++; $trow++; } // Append Blank Days while ( ( ( $days_in_month + $blank_days ) % 7 ) != 0 ) { $cal[ $curr_month ][ $trow ]['num'] = null; $days_in_month++; $trow++; } // Add 1 month to current date. $curr_date = date("Y-m-d", strtotime("+1 month", strtotime($curr_date))); } 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.