runeveryday Posted July 20, 2010 Share Posted July 20, 2010 $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. Quote Link to comment https://forums.phpfreaks.com/topic/208281-cant-understand-this-code/ Share on other sites More sharing options...
KevinM1 Posted July 20, 2010 Share Posted July 20, 2010 The code is checking to see if the month given has 28, 29, 30, or 31 days in it. See checkdate for more info. Quote Link to comment https://forums.phpfreaks.com/topic/208281-cant-understand-this-code/#findComment-1088529 Share on other sites More sharing options...
gwolgamott Posted July 20, 2010 Share Posted July 20, 2010 It is checking for the last day of any given month. Thus giving you the number of days in the month. Quote Link to comment https://forums.phpfreaks.com/topic/208281-cant-understand-this-code/#findComment-1088530 Share on other sites More sharing options...
runeveryday Posted July 20, 2010 Author Share Posted July 20, 2010 in this line , foreach (array_keys($week_days) as $day),why this array_keys($week_days) is not $week_days.why it use array_keys($week_days). Quote Link to comment https://forums.phpfreaks.com/topic/208281-cant-understand-this-code/#findComment-1088535 Share on other sites More sharing options...
gwolgamott Posted July 20, 2010 Share Posted July 20, 2010 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 Quote Link to comment https://forums.phpfreaks.com/topic/208281-cant-understand-this-code/#findComment-1088540 Share on other sites More sharing options...
runeveryday Posted July 20, 2010 Author Share Posted July 20, 2010 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; Quote Link to comment https://forums.phpfreaks.com/topic/208281-cant-understand-this-code/#findComment-1088550 Share on other sites More sharing options...
gwolgamott Posted July 20, 2010 Share Posted July 20, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/208281-cant-understand-this-code/#findComment-1088561 Share on other sites More sharing options...
runeveryday Posted July 21, 2010 Author Share Posted July 21, 2010 The code is checking to see if the month given has 28, 29, 30, or 31 days in it. See checkdate for more info. oh,where is 27 ? if $i=28, "if (!checkdate($month, 28, $year))" it will return 27($i-1). thank you. Quote Link to comment https://forums.phpfreaks.com/topic/208281-cant-understand-this-code/#findComment-1088897 Share on other sites More sharing options...
runeveryday Posted July 21, 2010 Author Share Posted July 21, 2010 anyone helps? Quote Link to comment https://forums.phpfreaks.com/topic/208281-cant-understand-this-code/#findComment-1088929 Share on other sites More sharing options...
gwolgamott Posted July 21, 2010 Share Posted July 21, 2010 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. Quote Link to comment https://forums.phpfreaks.com/topic/208281-cant-understand-this-code/#findComment-1089051 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.