can't understand this code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
everydayrun
Forum Commoner
Posts: 51
Joined: Wed Jan 20, 2010 1:30 am

can't understand this code

Post 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.
User avatar
LuaMadman
Forum Commoner
Posts: 35
Joined: Tue Jul 20, 2010 6:58 am

Re: can't understand this code

Post 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.
everydayrun
Forum Commoner
Posts: 51
Joined: Wed Jan 20, 2010 1:30 am

Re: can't understand this code

Post 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.
User avatar
LuaMadman
Forum Commoner
Posts: 35
Joined: Tue Jul 20, 2010 6:58 am

Re: can't understand this code

Post by LuaMadman »

array_keys($week_days) as day will print Sat,Sun,Mon while
$week_days as day would return 1,2,3
everydayrun
Forum Commoner
Posts: 51
Joined: Wed Jan 20, 2010 1:30 am

Re: can't understand this code

Post 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.
User avatar
LuaMadman
Forum Commoner
Posts: 35
Joined: Tue Jul 20, 2010 6:58 am

Re: can't understand this code

Post 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.
everydayrun
Forum Commoner
Posts: 51
Joined: Wed Jan 20, 2010 1:30 am

Re: can't understand this code

Post 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?
User avatar
LuaMadman
Forum Commoner
Posts: 35
Joined: Tue Jul 20, 2010 6:58 am

Re: can't understand this code

Post 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
everydayrun
Forum Commoner
Posts: 51
Joined: Wed Jan 20, 2010 1:30 am

Re: can't understand this code

Post 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.
User avatar
LuaMadman
Forum Commoner
Posts: 35
Joined: Tue Jul 20, 2010 6:58 am

Re: can't understand this code

Post by LuaMadman »

Yes, that's right.
Post Reply