Can't understand this code


$date = "10/01/2005"; 
$week_days = array("Sat"=>1, "Sun"=>2, "Mon"=>3,"Tue"=>4,"Wed"=>5, "Thu"=>6,"Fri"=>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","October 2005"); 
$smarty->assign("special_days", $days); 
$smarty->display("calendar.tpl"); 
function get_total_day($date) 
{ 
$time_stamp = strtotime($date); 
$month_ar = split("/", $date); 
$month = $month_ar[0]; 
$year = Date("Y",$time_stamp); 
for ($i=28; $i<33; $i++) 
{ 
if (!checkdate($month, $i, $year)){ 
return ($i - 1); 
} 
} 
} 
 

this code is from a php file,which use the smarty template. it’s a calendar of "October 2005 " i am a newbie of php and smarty,first, i want to know if you will be told to do a calendar as the above, how to think about that and read the code.
even i have read the above code, i am still don’t know how to do the calendar unless give a reference of it.

yup,i know it. i have understand the example.many many thanks to you.the example you made is easily to understand !Thumbs up!

Yup, that’s exactly what I mean :slight_smile:


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

var_dump(array_keys($week_days));
/* OUTPUT:
array(7) {
  [0]=>
  string(3) "Sat"
  [1]=>
  string(3) "Sun"
  [2]=>
  string(3) "Mon"
  [3]=>
  string(3) "Tue"
  [4]=>
  string(3) "Wed"
  [5]=>
  string(3) "Thu"
  [6]=>
  string(3) "Fri"
}
*/

var_dump(array_values($week_days));
/* OUTPUT:
array(7) {
  [0]=>
  int(1)
  [1]=>
  int(2)
  [2]=>
  int(3)
  [3]=>
  int(4)
  [4]=>
  int(5)
  [5]=>
  int(6)
  [6]=>
  int(7)
}
*/

An array consists of keys and values in the form key=>value
If you omit all keys the array will have numeric keys starting with 0.


$a = array('a','b','c');
var_dump(array_keys($a));
/* OUTPUT:
array(3) {
  [0]=>
  int(0)
  [1]=>
  int(1)
  [2]=>
  int(2)
}
*/

Does that make sense?

you mean that (“Mon” … “Sun”) is $days key ?

It is.


foreach (array_keys($week_days) as $day) {
  $days[] = $day; 
}

is a weird way of writing


$days = array_keys($week_days);

:slight_smile:

GOT it,many many thanks,what about this line " $days = $day; ",as i know,$day will be a value from Mon … Sun, now $days =array(“Mon” … “Sun”), i feel the foreach part that is useless.

i am very sorry,what your said is i know.but thank you all the same.

$starting_day = $week_days[Date(“D”,strtotime($date))] - 1;

this line which i can’t catch up with it. “Date(“D”,strtotime($date))” will return a value in this (Mon,Tue…Sun),namely,it will be like $week_days[Mon],which is an array,it can’t substrat one? why it wrote "$starting_day = $week_days[Date(“D”,strtotime($date))] - 1; “

Ok, let’s look at it step by step

strtotime($date)

returns a timestamp for the given date string, you can use this timestamp to use in the date function

Date(“D”,strtotime($date))

This will indeed result in a value from Mon … Sun, as a string. Suppose it will be “Mon”. Since we have established Date(“D”,strtotime($date)) is “Mon”, we can replace that piece of the code with “Mon” to get this:

$week_days[“Mon”] - 1;

Now, $week_days[“Mon”] is set to be 3, so we can replace $week_days[Date(“D”,strtotime($date))] with 3 to obtain

3-1

So, for “Mon”, $starting_day will be 2 :slight_smile:

Hi
Smarty->assign just assigns the value in the keyword which is in the calendar.tpl file. If you look at the file then you will see these “special_days” keyword which will be replaced by the value $smarty->assign(“special_days”, $days); here the value of the variable $days will be assigned to the special days keyword in the tpl file.
Other are just the normal php code.

What is it that you don’t understand exactly?

By the way, you can replace the whole get_total_day function using date(‘t’, strtotime($date));


$date = "10/01/2005"; 
$week_days = array(
  "Sat"=>1,
  "Sun"=>2,
  "Mon"=>3,
  "Tue"=>4,
  "Wed"=>5, 
  "Thu"=>6,
  "Fri"=>7
);
$total_day_of_month = date("t", strtotime($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","October 2005"); 
$smarty->assign("special_days", $days); 
$smarty->display("calendar.tpl"); 

I added curly brackets {} for readability, it doesn’t change what the code does :slight_smile: