Jump to content

Recommended Posts


$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[] = " ";
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.can't understand the get_total_day($date) function,why the $i value starts from 28,and ends on 32.any tips would be appreciated.

 

 

Link to comment
https://forums.phpfreaks.com/topic/208281-cant-understand-this-code/
Share on other sites

It's iterating through the array of keys and not the $week_days array.

array_keys() creates an array of keys from the array.

 

So in this case instead of going through the array of $week_days and getting 1 through 7 as the the $day variable it is passing the array_keys($week_days) so the $day variable in the foreach loop is actually being iterated with the following results instead: "Sat" "Sun" "Mon" "Tue" etc...

 

The programmer who did this foreach(array_keys($weeks_days) as $day most likely really didn't know you could iterate the keys as simply as the elements, all that was needed for what they did was this:

foreach($weeks_days as $day => $num_value)

That foreach will do the same thing as the one in that code.

 

http://us.php.net/manual/en/control-structures.foreach.php

http://us.php.net/manual/en/function.array-keys.php

 

many thanks,got it, now,$days[] is an arry,which value is sat,aun,mon..the rest i also don't catch up with it. i don't find the place that declare this $starting_day variable.

for ($i=0; $i<$starting_day; $i++)
$days[] = " ";
for ($i=1; $i< ($total_day_of_month+1); $i++)
$days[] = $i;

about three lines up

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

$starting_day  will be a number Here, the code to create the key for the array element this is wanting to subtract one from so you can run the loop up to that for making the spaces for the first calendar line I think... look up strtotime() & Date() to understand that. But it's getting a unix time stamp and converting it to a three letter day of the week for that time stamp... and this is from the $date variable it's creating that from.

 

 

What are you asking?

 

oh,where is 27 ? if  $i=28, "if (!checkdate($month, 28, $year))" it will return 27($i-1). thank you.

 

if (!checkdate($month, 28, $year))... This will not return 27 ever btw. Since every month has at least 28 it will be true with that second variable being 28. That if statement checks if that date is invalid. Any month, any year with 28 as the day is a valid date. To be technical this for loop could actually stare at 29 instead of 28 and still work. But no matter where you start the for loop as long as it's 29 or less will display the same result.

 

That if statement will return 28 for most years for the second month, 29 for leap years. Then return either 30 or 31 for the rest of the months.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.