Member Avatar for LastMitch

Hi

I want to create a simple calendar on Smarty. I attached an image on how it looks so far.

Here is my code calendar.php file:

<?php
require("libs/Smarty.class.php");
$smarty = new Smarty();

$date = "1/01/2013"; //January 01, 2013

$week_days = array("Sun"=>1, "Mon"=>2, "Tue"=>3, "Wed"=>4, "Thu"=>5, "Fri"=>6, "Sat"=>7);

$total_day_of_month = get_total_day($date);
$starting_day = $week_days[Date("D",strtotime($date))] - 1;
foreach (array_keys($week_days) as $day)
$days[] = $day;
for ($i=0; $i<$starting_day; $i++)
$days[] = "&nbsp;";
for ($i=1; $i< ($total_day_of_month+1); $i++)
$days[] = $i;

$smarty->assign("title","2013");
$smarty->assign("special_days", $days);
$smarty->assign("TABLE", array ("title" =>"January"));
$smarty->display("calendar.tpl");
$output = $smarty->fetch('calendar.tpl');

function get_total_day($date){
$time_stamp = strtotime($date);
$month_ar = explode("/", $date);
$month = $month_ar[0];
$year = Date("Y",$time_stamp);
for ($i=28; $i<33; $i++){
if (!checkdate($month, $i, $year)){
    return ($i - 1);
}}}
?>

Here is my calendar.tpl file:

<!DOCTYPE html> 
<html>
<head>
<title>Calendar</title>
</head>
<body>
<center><h1>{$title}</h1></center>
<table>
<tr>
<td> 
<center><b>{$TABLE.title}</b></center>
{html_table loop=$special_days cols=7}
</td>
</tr>
</table>
</body>
</html>

The script works for 1 month. In order to make adjustment I have to change $date = "1/01/2013"; //January 01, 2013 to a different month or year.

So I add or modify the code a little so instead of having 1 month I am trying to create 12 months kinda like a calendar for the whole year.

This is what I done so far:

This is my calendar.php file:

<?php
require("libs/Smarty.class.php");
$smarty = new Smarty();

$date = "1/01/2013"; //January 01, 2013

$months = array("January"=>1, "February "=>2, "March"=>3, "April"=>4, "May"=>5, "June"=>6, "July"=>7, "August"=>8, "September"=>9, "October"=>10, "November"=>11, "December"=>12);
$week_days = array("Sun"=>1, "Mon"=>2, "Tue"=>3, "Wed"=>4, "Thu"=>5, "Fri"=>6, "Sat"=>7);

$total_day_of_month = get_total_day($date);
$starting_day = $week_days[Date("D",strtotime($date))] - 1;
foreach (array_keys($week_days) as $day)
$days[] = $day;
for ($i=0; $i<$starting_day; $i++)
$days[] = "&nbsp;";
for ($i=1; $i< ($total_day_of_month+1); $i++)
$days[] = $i;

$smarty->assign("title","2013");
$smarty->assign("months", $months);
$smarty->assign("special_days", $days);
$smarty->assign("TABLE", array ("title" => $months));
$smarty->display("calendar.tpl");
$output = $smarty->fetch('calendar.tpl');

function get_total_day($date)
{
$time_stamp = strtotime($date);
$month_ar = explode("/", $date);
$month = $month_ar[0];
$year = Date("Y",$time_stamp);
for ($i=28; $i<33; $i++)
{
if (!checkdate($month, $i, $year)){
    return ($i - 1);
}}}
?>

My calendar.tpl file is still the same:

<!DOCTYPE html> 
<html>
<head>
<title>Calendar</title>
</head>
<body>
<center><h1>{$title}</h1></center>
<table>
<tr>
<td> 
<center><b>{$TABLE.title}</b></center>
{html_table loop=$special_days cols=7}
</td>
</tr>
</table>
</body>
</html>

Any Suggestions and explanation will help. I appreciate it. Thanks!

Untitled221

Since months and days belong together, I suggest you use a nested array. Then in your template you can use a foreach on the months, and in the loop create the html table for the days. If that is not what you mean, let me know.

$year = array (
    1 => array (
        'month' => 'January',
        'days' => $days
    ),
    2 => array (

    )
)

Also have a look at PHP's calendar functions. Might be useful.

Member Avatar for diafol
for ($i=28; $i<33; $i++){
if (!checkdate($month, $i, $year)){
    return ($i - 1);
}}}

You know that there's a "number of days in the month" format? date('t')

AFAIK there are no months with 32 days either ;) cal_days_in_month will just give you the number of days.

Member Avatar for LastMitch

@pritaeas

Thanks for the reply and explanation and example.

I will make some adjustment.

Member Avatar for LastMitch

@diafol

Thanks for the reply and explanation.

You're right about the 't'.

I will make an adjustment.

Member Avatar for LastMitch

Ok, So far I made some adjustment base on the explanations.

This is update changes I made on calendar.php file:

<?php
require("libs/Smarty.class.php");
$smarty = new Smarty();

$date = "1/01/2013"; //January 01, 2013

$year = array (
    1 => array (
    "month" => "January",
    "special_days" => $days),
    2 => array (
    "month" => "February",
    "special_days" => $days),
    3 => array (
    "month" => "March",
    "special_days" => $days),
    4 => array (
    "month" => "April",
    "special_days" => $days),
    5 => array (
    "month" => "May",
    "special_days" => $days),
    6 => array (
    "month" => "June",
    "special_days" => $days),
    7 => array (
    "month" => "July",
    "special_days" => $days),
    8 => array (
    "month" => "August",
    "special_days" => $days),
    9 => array (
    "month" => "September",
    "special_days" => $days),
    10 => array (
    "month" => "October",
    "special_days" => $days),
    11 => array (
    "month" => "November",
    "special_days" => $days),
    12 => array (
    "month" => "December",
    "special_days" => $days),
    );

$mons = array(1 => "January", 2 => "February", 3 => "March", 4 => "April", 5 => "May", 6 => "June", 7 => "July", 8 => "August", 9 => "September", 10 => "October", 11 => "November", 12 => "December");
$week_days = array("Sun"=>1, "Mon"=>2, "Tue"=>3, "Wed"=>4, "Thu"=>5, "Fri"=>6, "Sat"=>7);

$total_day_of_month = get_total_day($date);
$starting_day = $week_days[Date("D",strtotime($date))] - 1;
foreach (array_keys($week_days) as $day)
$days[] = $day;
for ($i=0; $i<$starting_day; $i++)
$days[] = "&nbsp;";
for ($i=1; $i< ($total_day_of_month+1); $i++)
$days[] = $i;

$total_month = get_total_month($date);
$starting_month = $mons[Date("F",strtotime($date))];
foreach (array_keys($mons) as $month)
$months[] = $month;
for ($i=0; $i<$starting_month; $i++)
$months[] = "&nbsp;";
for ($i=1; $i< ($total_month+1); $i++)
$months[] = $i;

$smarty->assign("title","2013");
$smarty->assign("month", $months);
$smarty->assign("special_days", $days);
$smarty->assign("TABLE", array ("title" => $months));
$smarty->display("calendar.tpl");
$output = $smarty->fetch('calendar.tpl');

function get_total_day($date){
$time_stamp = strtotime($date);
$month_ar = explode("/", $date);
$month = $month_ar[0];
$year = Date("Y",$time_stamp);
for ($i=28; $i<33; $i++){
if (!checkdate($month, $i, $year)){
    return ($i - 1);
}}}

function get_total_month($date){
$time_stamp = strtotime($date);
$month_ar = explode("/", $date);
$month = $month_ar[0];
$year = Date("Y",$time_stamp);
for ($i=1; $i<12; $i++){
if (!checkdate($month, $i, $year)){
    return ($i - 1);
}}}

?>

This is my update changes I made on calendar.tpl file:

<!DOCTYPE html> 
<html>
<head>
<title>Calendar</title>
</head>
<body>
<center><h1>{$title}</h1></center>
<table>
<tr>
<td> 
<center><b>{$TABLE.title}</b></center>
{html_table loop=$special_days cols=7}
{html_table loop=$month cols=12}
</td>
</tr>
</table>
</body>
</html>

I ran into alot of errors for the changes I made. I attach an image of the errors I got from the changes.

Any suggestion would appreciated. Thanks

Untitled121

Member Avatar for diafol

I'm not sure about your code, but to me, a calendar script should only need a few vars to set it up:

  1. number of days in the month
  2. day start of calendar (usu. Monday or Sunday)
  3. start day of month

Of course you'd need the target month and year too (or just year if you only want the year view).

Armed with those, you can then calculate the number of blanks required before and after the days.

Have you thought about building a calendar class? You seem to be hard coding a lot of info here, which could be dynamically produced. Your $year array for example can be produced dynamically. I can't see any info in it that can't be created from the vars mentioned. Just a thought.

Member Avatar for LastMitch

@diafol

Have you thought about building a calendar class? You seem to be hard coding a lot of info here, which could be dynamically produced. Your $year array for example can be produced dynamically. I can't see any info in it that can't be created from the vars mentioned. Just a thought.

This code is actually from a book. There was a few bugs on the script but I update certain terms to make it work.

You suggested that I should create a another class for the calendar?

I know you mention about this:

for ($i=28; $i<33; $i++){
if (!checkdate($month, $i, $year)){
    return ($i - 1);
}}}

The number 33 does represent the days but in a strange way.

If I make it less than 33 then the days won't appear on the calendar.

The more I play around with it, it has to do with this code

{html_table loop=$special_days cols=7}

I will try to create a class for the calendar.

Member Avatar for LastMitch

This is a Smarty Calendar Template. I created. It's done. It's not perfect. Any suggestion to improve. I can try to make some adjustment.

It took a couple weeks for me to figure it out. If anyone is interested in using this you can. Feel Free.

I attach an image on how it looks like.

This is the calendar.php file:

<?php
require("libs/Smarty.class.php");
$smarty = new Smarty();

// This where you can adjust the Month / Year.

$date1 = "1/01/2013"; // January 01, 2013
$date2 = "2/01/2013"; // February 01, 2013
$date3 = "3/01/2013"; // March 01, 2013
$date4 = "4/01/2013"; // April 01, 2013
$date5 = "5/01/2013"; // May 01, 2013
$date6 = "6/01/2013"; // June 01, 2013
$date7 = "7/01/2013"; // July 01, 2013
$date8 = "8/01/2013"; // August 01, 2013
$date9 = "9/01/2013"; // September 01, 2013
$date10 = "10/01/2013"; // October 01, 2013
$date11 = "11/01/2013"; // November 01, 2013
$date12 = "12/01/2013"; // December 01, 2013

// This the week array for each month

$week_days1 = array("Sun"=>1, "Mon"=>2, "Tue"=>3, "Wed"=>4, "Thu"=>5, "Fri"=>6, "Sat"=>7); // January 
$week_days2 = array("Sun"=>1, "Mon"=>2, "Tue"=>3, "Wed"=>4, "Thu"=>5, "Fri"=>6, "Sat"=>7); // February
$week_days3 = array("Sun"=>1, "Mon"=>2, "Tue"=>3, "Wed"=>4, "Thu"=>5, "Fri"=>6, "Sat"=>7); // March 
$week_days4 = array("Sun"=>1, "Mon"=>2, "Tue"=>3, "Wed"=>4, "Thu"=>5, "Fri"=>6, "Sat"=>7); // April
$week_days5 = array("Sun"=>1, "Mon"=>2, "Tue"=>3, "Wed"=>4, "Thu"=>5, "Fri"=>6, "Sat"=>7); // May
$week_days6 = array("Sun"=>1, "Mon"=>2, "Tue"=>3, "Wed"=>4, "Thu"=>5, "Fri"=>6, "Sat"=>7); // June
$week_days7 = array("Sun"=>1, "Mon"=>2, "Tue"=>3, "Wed"=>4, "Thu"=>5, "Fri"=>6, "Sat"=>7); // July
$week_days8 = array("Sun"=>1, "Mon"=>2, "Tue"=>3, "Wed"=>4, "Thu"=>5, "Fri"=>6, "Sat"=>7); // August
$week_days9 = array("Sun"=>1, "Mon"=>2, "Tue"=>3, "Wed"=>4, "Thu"=>5, "Fri"=>6, "Sat"=>7); // September
$week_days10 = array("Sun"=>1, "Mon"=>2, "Tue"=>3, "Wed"=>4, "Thu"=>5, "Fri"=>6, "Sat"=>7); // October 
$week_days11 = array("Sun"=>1, "Mon"=>2, "Tue"=>3, "Wed"=>4, "Thu"=>5, "Fri"=>6, "Sat"=>7); // November
$week_days12 = array("Sun"=>1, "Mon"=>2, "Tue"=>3, "Wed"=>4, "Thu"=>5, "Fri"=>6, "Sat"=>7); // December 

// January
$total_day_of_month1 = get_total_day1($date1);
$starting_day1 = $week_days1[Date("D",strtotime($date1))] - 1;
foreach (array_keys($week_days1) as $day1)
$days1[] = $day1;
for ($i=0; $i<$starting_day1; $i++)
$days1[] = "&nbsp;";
for ($i=1; $i< ($total_day_of_month1+1); $i++)
$days1[] = $i;

function get_total_day1($date1){
$time_stamp1 = strtotime($date1);
$month_ar1 = explode("/", $date1);
$month1 = $month_ar1[0];
$year1 = Date("Y",$time_stamp1);
for ($i=28; $i<33; $i++){
if (!checkdate($month1, $i, $year1)){
    return ($i - 1);
}}}

// February
$total_day_of_month2 = get_total_day2($date2);
$starting_day2 = $week_days2[Date("D",strtotime($date2))] - 1;
foreach (array_keys($week_days2) as $day2)
$days2[] = $day2;
for ($i=0; $i<$starting_day2; $i++)
$days2[] = "&nbsp;";
for ($i=1; $i< ($total_day_of_month2+1); $i++)
$days2[] = $i;

function get_total_day2($date2){
$time_stamp2 = strtotime($date2);
$month_ar2 = explode("/", $date2);
$month2 = $month_ar2[0];
$year2 = Date("Y",$time_stamp2);
for ($i=28; $i<33; $i++){
if (!checkdate($month2, $i, $year2)){
    return ($i - 1);
}}}

// March
$total_day_of_month3 = get_total_day3($date3);
$starting_day3 = $week_days3[Date("D",strtotime($date3))] - 1;
foreach (array_keys($week_days3) as $day3)
$days3[] = $day3;
for ($i=0; $i<$starting_day3; $i++)
$days3[] = "&nbsp;";
for ($i=1; $i< ($total_day_of_month3+1); $i++)
$days3[] = $i;

function get_total_day3($date3){
$time_stamp3 = strtotime($date3);
$month_ar3 = explode("/", $date3);
$month3 = $month_ar3[0];
$year3 = Date("Y",$time_stamp3);
for ($i=28; $i<33; $i++){
if (!checkdate($month3, $i, $year3)){
    return ($i - 1);
}}}

// April
$total_day_of_month4 = get_total_day4($date4);
$starting_day4 = $week_days4[Date("D",strtotime($date4))] - 1;
foreach (array_keys($week_days4) as $day4)
$days4[] = $day4;
for ($i=0; $i<$starting_day4; $i++)
$days4[] = "&nbsp;";
for ($i=1; $i< ($total_day_of_month4+1); $i++)
$days4[] = $i;

function get_total_day4($date4){
$time_stamp4 = strtotime($date4);
$month_ar4 = explode("/", $date4);
$month4 = $month_ar4[0];
$year4 = Date("Y",$time_stamp4);
for ($i=28; $i<33; $i++){
if (!checkdate($month4, $i, $year4)){
    return ($i - 1);
}}}

// May
$total_day_of_month5 = get_total_day5($date5);
$starting_day5 = $week_days5[Date("D",strtotime($date5))] - 1;
foreach (array_keys($week_days5) as $day5)
$days5[] = $day5;
for ($i=0; $i<$starting_day5; $i++)
$days5[] = "&nbsp;";
for ($i=1; $i< ($total_day_of_month5+1); $i++)
$days5[] = $i;

function get_total_day5($date5){
$time_stamp5 = strtotime($date5);
$month_ar5 = explode("/", $date5);
$month5 = $month_ar5[0];
$year5 = Date("Y",$time_stamp5);
for ($i=28; $i<33; $i++){
if (!checkdate($month5, $i, $year5)){
    return ($i - 1);
}}}

// June
$total_day_of_month6 = get_total_day6($date6);
$starting_day6 = $week_days6[Date("D",strtotime($date6))] - 1;
foreach (array_keys($week_days6) as $day6)
$days6[] = $day6;
for ($i=0; $i<$starting_day6; $i++)
$days6[] = "&nbsp;";
for ($i=1; $i< ($total_day_of_month6+1); $i++)
$days6[] = $i;

function get_total_day6($date6){
$time_stamp6 = strtotime($date6);
$month_ar6 = explode("/", $date6);
$month6 = $month_ar6[0];
$year6 = Date("Y",$time_stamp6);
for ($i=28; $i<33; $i++){
if (!checkdate($month6, $i, $year6)){
    return ($i - 1);
}}}

// July
$total_day_of_month7 = get_total_day7($date7);
$starting_day7 = $week_days7[Date("D",strtotime($date7))] - 1;
foreach (array_keys($week_days7) as $day7)
$days7[] = $day7;
for ($i=0; $i<$starting_day7; $i++)
$days7[] = "&nbsp;";
for ($i=1; $i< ($total_day_of_month7+1); $i++)
$days7[] = $i;

function get_total_day7($date7){
$time_stamp7 = strtotime($date7);
$month_ar7 = explode("/", $date7);
$month7 = $month_ar7[0];
$year7 = Date("Y",$time_stamp7);
for ($i=28; $i<33; $i++){
if (!checkdate($month7, $i, $year7)){
    return ($i - 1);
}}}

// August
$total_day_of_month8 = get_total_day8($date8);
$starting_day8 = $week_days8[Date("D",strtotime($date8))] - 1;
foreach (array_keys($week_days8) as $day8)
$days8[] = $day8;
for ($i=0; $i<$starting_day8; $i++)
$days8[] = "&nbsp;";
for ($i=1; $i< ($total_day_of_month8+1); $i++)
$days8[] = $i;

function get_total_day8($date8){
$time_stamp8 = strtotime($date8);
$month_ar8 = explode("/", $date8);
$month8 = $month_ar8[0];
$year8 = Date("Y",$time_stamp8);
for ($i=28; $i<33; $i++){
if (!checkdate($month8, $i, $year8)){
    return ($i - 1);
}}}

// September
$total_day_of_month9 = get_total_day9($date9);
$starting_day9 = $week_days9[Date("D",strtotime($date9))] - 1;
foreach (array_keys($week_days9) as $day9)
$days9[] = $day9;
for ($i=0; $i<$starting_day9; $i++)
$days9[] = "&nbsp;";
for ($i=1; $i< ($total_day_of_month9+1); $i++)
$days9[] = $i;

function get_total_day9($date9){
$time_stamp9 = strtotime($date9);
$month_ar9 = explode("/", $date9);
$month9 = $month_ar9[0];
$year9 = Date("Y",$time_stamp9);
for ($i=28; $i<33; $i++){
if (!checkdate($month9, $i, $year9)){
    return ($i - 1);
}}}

// October
$total_day_of_month10 = get_total_day10($date10);
$starting_day10 = $week_days10[Date("D",strtotime($date10))] - 1;
foreach (array_keys($week_days10) as $day10)
$days10[] = $day10;
for ($i=0; $i<$starting_day10; $i++)
$days10[] = "&nbsp;";
for ($i=1; $i< ($total_day_of_month10+1); $i++)
$days10[] = $i;

function get_total_day10($date10){
$time_stamp10 = strtotime($date10);
$month_ar10 = explode("/", $date10);
$month10 = $month_ar10[0];
$year10 = Date("Y",$time_stamp10);
for ($i=28; $i<33; $i++){
if (!checkdate($month10, $i, $year10)){
    return ($i - 1);
}}}

// November
$total_day_of_month11 = get_total_day11($date11);
$starting_day11 = $week_days11[Date("D",strtotime($date11))] - 1;
foreach (array_keys($week_days11) as $day11)
$days11[] = $day11;
for ($i=0; $i<$starting_day11; $i++)
$days11[] = "&nbsp;";
for ($i=1; $i< ($total_day_of_month11+1); $i++)
$days11[] = $i;

function get_total_day11($date11){
$time_stamp11 = strtotime($date11);
$month_ar11 = explode("/", $date11);
$month11 = $month_ar11[0];
$year11 = Date("Y",$time_stamp11);
for ($i=28; $i<33; $i++){
if (!checkdate($month11, $i, $year11)){
    return ($i - 1);
}}}

// December
$total_day_of_month12 = get_total_day12($date12);
$starting_day12 = $week_days12[Date("D",strtotime($date12))] - 1;
foreach (array_keys($week_days12) as $day12)
$days12[] = $day12;
for ($i=0; $i<$starting_day12; $i++)
$days12[] = "&nbsp;";
for ($i=1; $i< ($total_day_of_month12+1); $i++)
$days12[] = $i;

function get_total_day12($date12){
$time_stamp12 = strtotime($date12);
$month_ar12 = explode("/", $date12);
$month12 = $month_ar12[0];
$year12 = Date("Y",$time_stamp12);
for ($i=28; $i<33; $i++){
if (!checkdate($month12, $i, $year12)){
    return ($i - 1);
}}}

$smarty->assign("title","2013");
$smarty->assign("special_days1", $days1);
$smarty->assign("special_days2", $days2);
$smarty->assign("special_days3", $days3);
$smarty->assign("special_days4", $days4);
$smarty->assign("special_days5", $days5);
$smarty->assign("special_days6", $days6);
$smarty->assign("special_days7", $days7);
$smarty->assign("special_days8", $days8);
$smarty->assign("special_days9", $days9);
$smarty->assign("special_days10", $days10);
$smarty->assign("special_days11", $days11);
$smarty->assign("special_days12", $days12);
$smarty->assign("TABLE1", array ("title" =>"January"));
$smarty->assign("TABLE2", array ("title" =>"February"));
$smarty->assign("TABLE3", array ("title" =>"March"));
$smarty->assign("TABLE4", array ("title" =>"April"));
$smarty->assign("TABLE5", array ("title" =>"May"));
$smarty->assign("TABLE6", array ("title" =>"June"));
$smarty->assign("TABLE7", array ("title" =>"July"));
$smarty->assign("TABLE8", array ("title" =>"August"));
$smarty->assign("TABLE9", array ("title" =>"September"));
$smarty->assign("TABLE10", array ("title" =>"October"));
$smarty->assign("TABLE11", array ("title" =>"November"));
$smarty->assign("TABLE12", array ("title" =>"December"));
$smarty->display("calendar.tpl");
$output = $smarty->fetch('calendar.tpl');
?>

This is the calendar.tpl file:

<!DOCTYPE html> 
<html>
<head>
<title>Calendar</title>
</head>
<body>
<center><h1>{$title}</h1></center>
<table>
<tr>
<td> 
<center><b>{$TABLE1.title}</b></center>
{html_table loop=$special_days1 cols=7}
</td>
<td></td><td></td><td></td>
<td> 
<center><b>{$TABLE2.title}</b></center>
{html_table loop=$special_days2 cols=7}
</td>
<td></td><td></td><td></td>
<td> 
&nbsp;&nbsp;
<center><b>{$TABLE3.title}</b></center>
{html_table loop=$special_days3 cols=7}
</td>
<td></td><td></td><td></td>
<td> 
<center><b>{$TABLE4.title}</b></center>
{html_table loop=$special_days4 cols=7}
</td>
<td></td><td></td><td></td>
<td> 
<center><b>{$TABLE5.title}</b></center>
{html_table loop=$special_days5 cols=7}
</td>
<td></td><td></td><td></td>
<td>
&nbsp;&nbsp;
<center><b>{$TABLE6.title}</b></center>
{html_table loop=$special_days6 cols=7}
</td>
</tr>
</table>
<table>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
<tr><td></td></tr>
</table>
<table>
<tr>
<td> 
<center><b>{$TABLE7.title}</b></center>
{html_table loop=$special_days7 cols=7}
</td>
<td></td><td></td><td></td>
<td> 
<center><b>{$TABLE8.title}</b></center>
{html_table loop=$special_days8 cols=7}
</td>
<td></td><td></td><td></td>
<td> 
<center><b>{$TABLE9.title}</b></center>
{html_table loop=$special_days9 cols=7}
</td>
<td></td><td></td><td></td>
<td> 
<center><b>{$TABLE10.title}</b></center>
{html_table loop=$special_days10 cols=7}
</td>
<td></td><td></td><td></td>
<td> 
<center><b>{$TABLE11.title}</b></center>
{html_table loop=$special_days11 cols=7}
</td>
<td></td><td></td><td></td>
<td> 
<center><b>{$TABLE12.title}</b></center>
{html_table loop=$special_days12 cols=7}
</td>
</tr>
</table>
</body>
</html>

calendar3

Member Avatar for diafol

Here's one of my first OOP routines from a while back - it's pretty spaghetti-ish, but it seems to work. It doesn't use Smarty, but as it returns an array of 'divs', no reason why it couldn't.

<style>
    table{
        border-collapse:collapse;   
    }

    .month table td, .month table th{
        border: black solid 1px;
        text-align:center;  
    }
    .month h3{
        text-align:center;  
    }
    .month{
        width: 200px;
        display: inline-block;  
    }
</style>

<?php
class diaCal{

    private $numMonths;             //calculated from DTOs
    private $calHeader;             //day headings for calendar
    private $bDTO;                  //start DTO
    private $dayArray = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
    private $options = array();     //keep current options
    private $allowedOptions = array('startDay','dayFormat','monthTitleFormat');  //settable option items
    private $calArray = array();    //output container

    public function __construct($startDate=NULL,$endDate=NULL)
    {
        //Dates in unix format: Y-m-d
        $begin = ($startDate) ? $startDate : date('Y-m-d');
        $end = ($endDate && $endDate > $startDate) ? $endDate : $begin;

        //CREATE DATETIME Objects for calculating differences
        $bDTO = new DateTime($begin);
        $eDTO = new DateTime($end);
        $bDTO->setDate($bDTO->format('Y'), $bDTO->format('n'), 1);
        $eDTO->setDate($eDTO->format('Y'), $eDTO->format('n'), 2);

        //CALCULATE MONTHS TO DISPLAY
        $interval = $bDTO->diff($eDTO);
        $months = $interval->format('%m') + 1;  //Get months difference (11 or less)
        $months += $interval->format('%y') * 12;//Get years difference between dates
        $this->numMonths = $months;             //The number of months to display

        //INITIALIZE default options        
        $this->options['startDay'] = 0;         //First Calendar Day: 0 = Sunday, 1 = Monday, etc.
        $this->options['dayFormat'] = 3;        //Calendar Day Abbrev: 0 = SundayMonday..., 1 = SMTWTFS, 3 = SunMonTue..., etc  
        $this->options['monthTitleFormat'] = 'M, Y'; //date formats, e.g. F or M for month, Y or y for year (optional)
        $this->options['monthTitleTag'] = 'h3'; //HTML tag for month title, e.g. h2, h3, h4...

        //Add to private var for later use
        $this->bDTO = $bDTO;
    }

    public function setOptions($key, $value)
    {
        //Set option values - but only those allowed in $allowedOptions
        if(in_array($key, $this->allowedOptions))$this->options[$key] = $value; 
    }

    private function addMonth()
    {
        //Get Essential Items from DateTime object
        $firstDayOfMonth = $this->bDTO->format('w');
        $numDaysInMonth = $this->bDTO->format('t');

        //Calculate blank cells before and after valid days and then create the html
        $leadingBlanks = $this->getFirstBlanks($firstDayOfMonth);
        $trailingBlanks = $this->getLastBlanks($leadingBlanks, $numDaysInMonth);
        $firstRepeat = ($leadingBlanks) ? "\n\t\t\t<tr>" . str_repeat("\n\t\t\t\t<td></td>", $leadingBlanks) : '';
        $lastRepeat = ($trailingBlanks) ? str_repeat("\n\t\t\t\t<td></td>", $trailingBlanks) . "\n\t\t\t</tr>" : '';

        //Build output table HTML       
        $table = "\n<div class='month'>" . $this->getTitle() . "\n\t<table>" . $this->calHeader . $firstRepeat;
        $count = $leadingBlanks;

        for($x = 1; $x <= $numDaysInMonth; $x++){
            if($count == 0)$table .= "\n\t\t\t<tr>";
            $table .= "\n\t\t\t\t<td>$x</td>";
            if($count == 6)$table .= "\n\t\t\t</tr>";
            $count++;
            if($count == 7)$count = 0;
        }
        $table .= $lastRepeat . "\n\t\t</tbody>\n\t</table>\n</div>";
        return $table;
    }

    //Calculate leading blank cells 
    private function getFirstBlanks($firstDayOfMonth)
    {
        $startPos = $firstDayOfMonth - $this->options['startDay'];
        if($startPos < 0)$startPos += 8;
        return ($startPos != 0) ? $startPos : 0;
    }

    //Calculate trailing blank cells
    private function getLastBlanks($leadingBlanks, $numDaysInMonth)
    {
        $filledSoFar = intval($leadingBlanks + $numDaysInMonth);
        return (ceil($filledSoFar / 7) * 7) - $filledSoFar ;
    }

    //Create HTML table header containing day abbreviations
    private function makeCalHeader()
    {
        $dayFormat = $this->options['dayFormat'];
        $i = $this->options['startDay'];
        $calHeader = "\n\t\t<thead>\n\t\t\t<tr>";
        for($x=0;$x<=6;$x++){
            $dayLabel = ($dayFormat) ? substr($this->dayArray[$i],0,$dayFormat) : $this->dayArray[$i];
            $calHeader .= "\n\t\t\t\t<th>" . $dayLabel . "</th>";
            $i++;
            if($i == 7)$i = 0;
        }
        $calHeader .= "\n\t\t\t</tr>\n\t\t</thead>\n\t\t<tbody>";
        $this->calHeader =  $calHeader;
    }

    //Create HTML month/year title for each month
    private function getTitle()
    {
        $output = "\n\t<{$this->options['monthTitleTag']}>" . $this->bDTO->format($this->options['monthTitleFormat']) . "</{$this->options['monthTitleTag']}>";
        return $output;
    }

    //Call to create month tables
    public function getCal()
    {
        $this->makeCalHeader();
        $num = $this->numMonths;
        for($x = 0;$x < $num; $x++){
            $calArray[] = $this->addMonth();
            $this->bDTO->add(new DateInterval('P1M'));
        }
        return $calArray;       
    }

}

$d = new diaCal('2012-01-01','2013-01-27');
$d->setOptions('startDay', 1); //start on Monday
$d->setOptions('dayFormat', 1);
$d->setOptions('monthTitleTag', 'h4');
$d->setOptions('monthTitleFormat', 'F - Y');
foreach($d->getCal() as $month) echo $month;    

?>
commented: Thanks for sharing! +9
Member Avatar for LastMitch

@diafol

Here's one of my first OOP routines from a while back - it's pretty spaghetti-ish, but it seems to work. It doesn't use Smarty, but as it returns an array of 'divs', no reason why it couldn't.

Thanks for sharing! Yes, I ran your code and it does look nice. My OOP is not as good as you. But I can learn something from it. Thanks.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.