Win a new Mac Book Air laptop

Archive for September, 2007

A simple PHP calendar function

Friday, September 21st, 2007

Recently I needed a calendar to display on a site. I looked around for something I could just cut and paste and drop in but I couldn’t find anything, but picked up something I had started about six months earlier and which had then fallen by the wayside. Now calendars aren’t actually very complicated, but they are a bit of a fiddle. The aim of this calendar function is therefore just to produce a skeleton calendar that I can drop into anything, you can stick anything you want in a calendar cell (day) - it doesn’t matter because all the code for content lives outside the calendar. There are css hooks for all the various cells, columns etc. so you can format it exactly how you want.

Calendar Screenshot

I wrote this as a CakePHP helper, but at the end of the day it is just a function and there is nothing Cake specific about it.

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
 
<?php
 
function calendar($year = '', $month = '', $data = '', $base_url ='')
		{
			$str = '';
			$month_list = array('january', 'febuary', 'march', 'april', 'may', 'june', 'july', 'august', 'september', 'october', 'november', 'december');
			$day_list = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun');
			$day = 1;
			$today = 0;
 
			if($year == '' || $month == '')// just use current yeear & month
				{
					$year = date('Y');
					$month = date('M');
 
				}
 
 
			$flag = 0;
 
			for($i = 0; $i < 12; $i++)
				{
					if(strtolower($month) == $month_list[$i])
						{
							if(intval($year) != 0)
								{
									$flag = 1;
									$month_num = $i + 1;
									break;
								}
						}
				}
 
 
			if($flag == 0)
				{
					$year = date('Y');
					$month = date('F');
					$month_num = date('m');
				}
 
 
 
			$next_year = $year;
			$prev_year = $year;
 
			$next_month = intval($month_num) + 1;
			$prev_month = intval($month_num) - 1;
 
			if($next_month == 13)
				{
					$next_month = 'january';
					$next_year = intval($year) + 1;
				}
			else
				{
					$next_month = $month_list[$next_month -1];
 
				}
 
			if($prev_month == 0)
				{
					$prev_month = 'december';
					$prev_year = intval($year) - 1;
				}
			else
				{
					$prev_month = $month_list[$prev_month - 1];
				}
 
 
 
			if($year == date('Y') && strtolower($month) == strtolower(date('F')))
				{	// set the flag that shows todays date but only in the current month - not past or future...
					$today = date('j');
				}
 
			$days_in_month = date("t", mktime(0, 0, 0, $month_num, 1, $year));
 
			$first_day_in_month = date('D', mktime(0,0,0, $month_num, 1, $year)); 
 
 
 
			$str .= '<table class="calendar">';
 
			$str .= '<thead>';
 
			$str .= '<tr><th class="cell-prev"><a href="' . $base_url . '/' . $prev_year . '/' . $prev_month . '">prev</a></th><th colspan="5">' . ucfirst($month) . ' ' . $year . '</th><th class="cell-next"><a href="' . $base_url . '/' . $next_year . '/' . $next_month . '">next</a></th></tr>';
 
 
 
			$str .= '<tr>';
				for($i = 0; $i < 7;$i++)
					{
						$str .= '<th class="cell-header">' . $day_list[$i] . '</th>';
					}
			$str .= '</tr>';
 
			$str .= '</thead>';
 
			// get the first day of the month
 
 
			$str .= '<tbody>';
 
 
 
				while($day < $days_in_month)
					{
						$str .= '<tr>';
 
 
 
								for($i = 0; $i < 7; $i ++)
									{
 
										$cell = '&nbsp;';
 
										if(isset($data[$day]))
											{
												$cell = $data[$day];
											}
 
										$class = '';
 
										if($i > 4)
											{
												$class = ' class="cell-weekend" ';
											}
 
 
										if($day == $today)
											{
												$class = ' class="cell-today" ';
											}
 
										if(($first_day_in_month == $day_list[$i] || $day > 1) && ($day < $days_in_month))
											{
												$str .= '<td ' . $class . '><div class="cell-number">' . $day . '</div><div class="cell-data">' . $cell . '</div></td>';
												$day++;
											}
										else
											{
 
														$str .= '<td ' . $class . '>&nbsp;</td>';
											}
									}
 
						$str .= '</tr>';
					}
 
 
			$str .= '</tbody>';
 
			$str .= '</table>';
 
			return $str;
		}
 
?>

The function parameters are as follows:

  • $year - expects a year in 4 digit e.g. 2007
  • $month - expects a month in english e.g. january
  • $data - an array containing the data for each day of the month e.g.

    $data[2] = ‘This is an entry for the 2nd day of the month’;
    $data[24] = ‘A link for the 24th of the month‘;

    The data is any HTML you want - it is up to you to generate it yourself before you hand it to the calendar.

  • $base_url - the url to send the back / foward links on to i.e. the address of page (the calendar expects to be in a mod re-written situation e.g. www.flipflops.org/calendar/2008/june)

CSS

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
 
/* calendar CSS */
 
table.calendar {width: auto; border: 1px solid #cccccc; border-collapse: collapse; margin: 0px; padding: 0px; background-color: #ffffff;}
table.calendar th {background-color: #eeeeee; text-transform: none; color: #444444; padding: 4px; text-align: center; border: 1px solid #eeeeee;}
 
table.calendar th.cell-prev {text-align: left;}
table.calendar th.cell-next {text-align: right;}
table.calendar th.cell-header {width: 70px; border-bottom: 1px solid #cccccc;}
table.calendar td.cell-today {background-color: #e2e8f6;} /* today in the current month */
table.calendar td.cell-weekend {background-color: #F3F5EB;}
table.calendar td {border: 1px solid #cccccc;}
 
table.calendar td div.cell-number {text-align: right; font-size: 8px; color: #444444; display: block;}
table.calendar td div {display: block; font-size: 10px; text-align: left;}
table.calendar thead th {border: 1px solid #cccccc;}

I use this as a CakePHP helper - if anyone is interested I’ll put up the supporting code showing how it might be used in a view / controller situation.

Electricity

Tuesday, September 18th, 2007

I saw two videos today both of them were disturbing in thier own ways the similarity - electricity.

The first one shows a guy with the worlds fastest electric drag bike - crashing it.

The second clip shows a student getting zapped with a tazer gun at a rally for US presidential candidate John Kerry.

Portfolio 2004 -2007

Friday, September 14th, 2007

A few sites from my Portfolio 2004 - 2007. Working as Senior Developer for COSMIC. I developed a PHP CMS framework called Rocket which is as the core of most of these sites.

The projects include:

  • E-commerce
  • Content Management Systems
  • Custom Programming & Database Integration

Stream :: Photographs

Friday, September 14th, 2007

We had the private view for the show last Friday and had quite a good turnout - over 250 people saw the piece last weekend and I had some great feedback.

(Click on the image to see the gallery of the photographs)

CakePHP : clear app/tmp/model when you set DEBUG 0

Thursday, September 6th, 2007

I’m about to hand over a CakePHP based CRM system to the clients. Happy that everything was working as it should, I set DEBUG to 0. I immediately noticed that almost all the data had disapeared from the edit views and the detail views (although not the index views curriously eneough).

It took me quite a while to find the solution. It turns out that you have to delete all the files in the /app/tmp/model/ directory.

See CakePHP group Setting debug = 0 kills my app?

order clomid viagra online review cialis from canada cheap generic viagra compare cialis prices online buy cheap acomplia buy viagra no rx find discount viagra online cheap accutane online lasix pills drug cialis online purchase order discount viagra order viagra from canada cheap lasix online online propecia viagra information accutane prices cheap generic acomplia levitra generic cialis cost levitra without prescription propecia prescription buy acomplia cheap acomplia prices acomplia cheap discount viagra purchase cialis no rx buy cialis us synthroid buy cheap cialis cheap zithromax cialis drug lowest price lasix clomid online cheap order acomplia online viagra online cheapest soma prices order cialis no rx cialis without a prescription cheap clomid buy viagra without prescription cheap generic accutane buy propecia without prescription cialis free sample propecia pharmacy buy viagra from us zithromax cheap buy levitra generic propecia no rx viagra cialis tablets cialis without prescription generic viagra cheap generic accutane discount cialis online clomid cheap buy discount viagra cheap viagra on internet overnight viagra viagra uk buy viagra from canada buy generic accutane viagra no prescription zithromax pills cheapest synthroid prices discount synthroid where to buy zithromax cheapest zithromax prices buy soma without prescription accutane no prescription cheap levitra tablets find cheap cialis online levitra pills