A Calendar Element for CakePHP
Here’s a quick calendar element I whipped up for a CakePHP application I’m writing. I needed to use the calendar in a number of different places, so creating a view element for it in Cake made the most sense.
<<< March 2009 >>>
| Su | Mo | Tu | We | Th | Fr | Sa |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | 7 |
| 8 | 9 | 10 | 11 | 12 | 13 | 14 |
| 15 | 16 | 17 | 18 | 19 | 20 | 21 |
| 22 | 23 | 24 | 25 | 26 | 27 | 28 |
| 29 | 30 | 31 |
Download the source (zipped .ctp file) here: CakePHP Calendar Element
The code is pretty simple. Pass the element a year and a month when rendering it and it will render that month in a simple table. It also accepts two additional arguments, a day link and a month link. The day link allows you to specify what the base URL for each day should be, and similarly, the month link specifies the base URL for traversing months.
Drop the calendar.ctp into your elements directory in your Cake app (usually /app/views/elements) and render it an any view you’d like. Below is an example of rendering the element with all of its parameters:
<?php
$this->renderElement('calendar', array(
'year' => 2009,
'month' => '11',
'month_link' => '/controller/showmonth/',
'day_link' => '/controller/showday/'
));
?>
The month link and day link provide the base URLs for linking to additional months and days, allowing you to modify the element for particular controller actions.
Feel free to use this component for anything you’d like, no strings attached. If you make any improvements or enhancements to it, be sure to share!
Many Thanks. Just what I needed.
Info: code works with another “)” at the end and without the last “,”!
Whoops! Thanks for catching that. I’ve fixed the original code.
That’s what I’m looking for
Hey,
thanks for this great calendar component. It’s great
Would you mind post a piece of code which shows the calendar starting on Monday, not on Sunday? I’M not that good in PHP to modify the code…
I really appreciate any help
Thank you very much,
Benny
This looks great! I need a simple calendar system for a project I am working on (to display local events by month and day), and this looks like it will be a great starting point that will save me some time.
In the example the echo command is missing before $this->renderElement.
An thanks for sharing this element, it opened my mind on a project I’m working on.
thanx man.
It was useful for me.
Do you have the style sheet for this element, it is not attached to the download.
Thanks
I don’t have a stylesheet available for this; the one you see above is simply using the stylesheet for my current WordPress theme.