craygo Posted September 20, 2007 Share Posted September 20, 2007 I have a function that gives me the difference between 2 dates. I also have another function which compares a couple things and uses the first function. How do I call one function from within another. Here are the 2 functions. <?php function datediff($interval, $datefrom, $dateto, $using_timestamps = false) { /* $interval can be: yyyy - Number of full years q - Number of full quarters m - Number of full months y - Difference between day numbers (eg 1st Jan 2004 is "1", the first day. 2nd Feb 2003 is "33". The datediff is "-32".) d - Number of full days w - Number of full weekdays ww - Number of full weeks h - Number of full hours n - Number of full minutes s - Number of full seconds (default) */ if (!$using_timestamps) { $datefrom = strtotime($datefrom, 0); $dateto = strtotime($dateto, 0); } $difference = $dateto - $datefrom; // Difference in seconds switch($interval) { case 'yyyy': // Number of full years $years_difference = floor($difference / 31536000); if (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom), date("j", $datefrom), date("Y", $datefrom)+$years_difference) > $dateto) { $years_difference--; } if (mktime(date("H", $dateto), date("i", $dateto), date("s", $dateto), date("n", $dateto), date("j", $dateto), date("Y", $dateto)-($years_difference+1)) > $datefrom) { $years_difference++; } $datediff = $years_difference; break; case "q": // Number of full quarters $quarters_difference = floor($difference / 8035200); while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($quarters_difference*3), date("j", $dateto), date("Y", $datefrom)) < $dateto) { $months_difference++; } $quarters_difference--; $datediff = $quarters_difference; break; case "m": // Number of full months $months_difference = floor($difference / 2678400); while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($months_difference), date("j", $dateto), date("Y", $datefrom)) < $dateto) { $months_difference++; } $months_difference--; $datediff = $months_difference; break; case 'y': // Difference between day numbers $datediff = date("z", $dateto) - date("z", $datefrom); break; case "d": // Number of full days $datediff = floor($difference / 86400); break; case "w": // Number of full weekdays $days_difference = floor($difference / 86400); $weeks_difference = floor($days_difference / 7); // Complete weeks $first_day = date("w", $datefrom); $days_remainder = floor($days_difference % 7); $odd_days = $first_day + $days_remainder; // Do we have a Saturday or Sunday in the remainder? if ($odd_days > 7) { // Sunday $days_remainder--; } if ($odd_days > 6) { // Saturday $days_remainder--; } $datediff = ($weeks_difference * 5) + $days_remainder; break; case "ww": // Number of full weeks $datediff = floor($difference / 604800); break; case "h": // Number of full hours $datediff = floor($difference / 3600); break; case "n": // Number of full minutes $datediff = floor($difference / 60); break; default: // Number of full seconds (default) $datediff = $difference; break; } return $datediff; } function gametime($now, $lastgame){ $gt = array(); $game_time_dif = ($now - $lastgame); if($game_time_dif < 3601){ $gt[0] = "mins ago"; $gt[1] = abs(datediff('n', $now, $lastgame, true)); } else { if($game_time_dif > 3600 && $game_time_dif < 86400){ $gt[0] = "hrs ago"; $gt[1] = abs(datediff('h', $now, $lastgame, true)); } else { $gt[0] = "days ago"; $gt[1] = abs(datediff('d', $now, $lastgame, true)); } } if($gt[1] > 9 && $gt[0] == "days ago"){ $gt[2] = "<font color=red>"; } else { $gt[2] = "<font color=black>"; } return $gt; } ?> thanks for the help ray Quote Link to comment https://forums.phpfreaks.com/topic/70003-solved-call-function-inside-another-function/ Share on other sites More sharing options...
GingerRobot Posted September 20, 2007 Share Posted September 20, 2007 Im not sure i understand what the problem is. As far as i can see, the functions work. If i use: <?php $gt = gametime(3600,3540); echo $gt[1].' '.$gt[0]; ?> After having defined your functions, then i get the output 1 mins ago, which i presume is the correct output. Do you think you could explain a bit more? Quote Link to comment https://forums.phpfreaks.com/topic/70003-solved-call-function-inside-another-function/#findComment-351591 Share on other sites More sharing options...
craygo Posted September 20, 2007 Author Share Posted September 20, 2007 Jesus I got to be a retard. Thanks for the quick reply ginger. I forgot to store the function in a variable. i use gametime($now, $last_game); $gdiff = $gt[0]; instead of $gt = gametime($now, $last_game); $gdiff = $gt[0]; thanks for slapping me across the head!!! Ray Quote Link to comment https://forums.phpfreaks.com/topic/70003-solved-call-function-inside-another-function/#findComment-351603 Share on other sites More sharing options...
GingerRobot Posted September 20, 2007 Share Posted September 20, 2007 Haha, no worries. Always the simple little bits that catch you out Quote Link to comment https://forums.phpfreaks.com/topic/70003-solved-call-function-inside-another-function/#findComment-351609 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.