Display Calendar In Html

lua-users home
wiki

Showing revision 4
Displays a Calendar in HTML. This is ideal for 'Kepler' (Lua's equivalent of PHP).

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

RecentChanges · preferences
edit · history · current revision
Edited January 1, 2007 10:16 pm GMT (diff)