Page 1 of 1

can't understand this code

Posted: Tue Jul 20, 2010 7:05 am
by everydayrun

Code: Select all

$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.can't understand the get_total_day($date) function,why the $i value starts from 28,and ends on 32.
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;

what about these mean? any tips would be appreciated.

Re: can't understand this code

Posted: Tue Jul 20, 2010 7:34 am
by LuaMadman
I'm quite new to php.
So i might be wrong.
But I belive that the function checks if the month has 30 or 31 days
it starts on 28 beacuse feb might have 28 days
It 's unnecessary to check if days 1 to 27 exsits, beacuse they always will, 28-31 might not.

Re: can't understand this code

Posted: Tue Jul 20, 2010 7:46 am
by everydayrun
you're right. thank you.in this line , foreach (array_keys($week_days) as $day),why this "array_keys($week_days)"is not $week_days.

Re: can't understand this code

Posted: Tue Jul 20, 2010 7:52 am
by LuaMadman
array_keys($week_days) as day will print Sat,Sun,Mon while
$week_days as day would return 1,2,3

Re: can't understand this code

Posted: Tue Jul 20, 2010 8:01 am
by everydayrun
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;
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.

Re: can't understand this code

Posted: Tue Jul 20, 2010 8:04 am
by LuaMadman

Code: Select all

$starting_day = $week_days[Date("D",strtotime($date))] - 1; // Get week day of date and then get nr from array
foreach (array_keys($week_days) as $day) // get keys in array (Sat,Sun,Mon)
	$days[] = $day; // Add day in to days array
for ($i=0; $i<$starting_day; $i++) // Count from 0 to starting day (Fri = 7)
	$days[] = "&nbsp;"; // Add space to array
for ($i=1; $i< ($total_day_of_month+1); $i++) // count from 1 to nr of days in mount + 1
this is how i understad the code.

Re: can't understand this code

Posted: Wed Jul 21, 2010 1:30 am
by everydayrun
LuaMadman wrote:I'm quite new to php.
So i might be wrong.
But I belive that the function checks if the month has 30 or 31 days
it starts on 28 beacuse feb might have 28 days
It 's unnecessary to check if days 1 to 27 exsits, beacuse they always will, 28-31 might not.
oh,but if $i=28,then it will return 27, that is the for loop will return 27,28,29,30,32,how the function only checks if the month has 30 or 31 days?

Re: can't understand this code

Posted: Wed Jul 21, 2010 1:58 am
by LuaMadman
it has !checkdate() with tells it if dates does not exist then do code bellow. in this code $i - 1
So if $i=28 it will check to see if 28 exist, confirm that it does, move on to 29, confirm that' it don't exist, and then do $i - 1 to return that 28 was the last found.
or if $i=31 it will check that 28,29,30 exist, they all do. if 31 does not exists, it does $i - 1 and return 30. if 31 exists, it will check if 32 does, it dosen't, so it do $i-1 and return 31

Re: can't understand this code

Posted: Wed Jul 21, 2010 2:54 am
by everydayrun
LuaMadman wrote:it has !checkdate() with tells it if dates does not exist then do code bellow. in this code $i - 1
So if $i=28 it will check to see if 28 exist, confirm that it does, move on to 29, confirm that' it don't exist, and then do $i - 1 to return that 28 was the last found.
or if $i=31 it will check that 28,29,30 exist, they all do. if 31 does not exists, it does $i - 1 and return 30. if 31 exists, it will check if 32 does, it dosen't, so it do $i-1 and return 31
i know,i know...many many thanks to you! in this example,the month is Jan,so the $i=32 will not exist, it will return 31.am i right.

Re: can't understand this code

Posted: Wed Jul 21, 2010 3:51 am
by LuaMadman
Yes, that's right.