Display Calendar In Html |
|
This code requires the get_date_parts
, get_days_in_month
and get_day_of_week
functions from DayOfWeekAndDaysInMonthExample.
-- based on original code from sam_lie function show_calendar(cdate) local yy, mm, dd = get_date_parts(cdate) local month_days = get_days_in_month(mm, yy) local day_week = get_day_of_week(1, mm, yy) --- day in which the calendar day start.. 1=Sunday, 2="Monday" local day_start = 2 local days_of_week = {{ "Sun", 1 }, { "Mon", 2 } , { "Tue", 3 }, { "Wed", 4 }, { "Thu", 5 }, { "Fri", 6 }, { "Sat", 7 }} local days_of_week_ordered = {} for k=1, 7 do p = k+day_start-1 if (p>7) then p=p-7 end v = {} v.dayname = days_of_week[p][1] v.daynum = days_of_week[p][2] table.insert(days_of_week_ordered, v) end local out = "<h3>" .. cdate .. "</h3>" out = out .. "<table border='1' width='80%' cellspacing='2' cellpadding='5'>" out = out .. "<tr>" for i,v in ipairs(days_of_week_ordered) do out = out .. "<td>" .. v.dayname .. "</td>" if (day_week == v.daynum) then d = - i + 2 end end out = out .. "</tr>" while (d < month_days) do out = out .. "<tr>" for i,v in ipairs(days_of_week) do if (d>=1 and d <=month_days) then if (d==dd) then out = out .. "<td bgcolor='#FFFF99'>" .. d .. "</td>" else out = out .. "<td>" .. d .. "</td>" end else out = out .. "<td> </td>" end d = d + 1 end out = out .. "</tr>" end out = out .. "</table>" return out end
Here is a test:
print(show_calendar('2007-01-01')) -- test
Alternate version by RichardWarburton that doesn't have the dependencies above:
local print = function(...) SAPI.Response.write (table.concat(arg)) end -- suggestion if using Kepler function displayMonth(month, year, weekStart) local t,wkSt = os.time{year=year, month=month+1, day=0}, weekStart or 1 local d = os.date("*t", t) local mthDays, stDay = d.day,(d.wday-d.day-wkSt+1)%7 print('<table class="month">\n<caption>', os.date('%B',t), '</caption>\n<tr>') for x=0,6 do print(os.date("<th>%a</th>", os.time{year=2006, month=1, day=x+wkSt})) end print('</tr>\n<tr>', string.rep('<td></td>', stDay)) for x=1,mthDays-1 do print("<td>", x, "</td>", (x+stDay)%7==0 and "</tr>\n<tr>" or "") end print('<td>', mthDays, '</td>', string.rep('<td></td>', (7-(mthDays+stDay))%7), "</tr>\n</table>\n") end