Jump to content

PHP simple calendar array


biggieuk

Recommended Posts

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!

Link to comment
Share on other sites

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.  :confused:

 

I have attatched a sample of the mini calendar I am going for.

 

[attachment deleted by admin]

Link to comment
Share on other sites

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)));
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.