defroster Posted July 24, 2010 Share Posted July 24, 2010 I am having huge problems with this, I don't get any error message and the html output just freezes. Any idea what can be wrong? $pasttime=@get_TimeAgo($row['dateposted'],$dateto=-1); echo "time:" . $pasttime. "since post"; Function: function TimeAgo($datefrom,$dateto=-1) { $datefrom = strtotime($datefrom); // Defaults and assume if 0 is passed in that // its an error rather than the epoch if($datefrom<=0) { return "A long time ago"; } if($dateto==-1) { $dateto = time(); } // Calculate the difference in seconds betweeen // the two timestamps $difference = $dateto - $datefrom; // If difference is less than 60 seconds, // seconds is a good interval of choice if($difference < 60) { $interval = "s"; } // If difference is between 60 seconds and // 60 minutes, minutes is a good interval elseif($difference >= 60 && $difference<60*60) { $interval = "n"; } // If difference is between 1 hour and 24 hours // hours is a good interval elseif($difference >= 60*60 && $difference<60*60*24) { $interval = "h"; } // If difference is between 1 day and 7 days // days is a good interval elseif($difference >= 60*60*24 && $difference<60*60*24*7) { $interval = "d"; } // If difference is between 1 week and 30 days // weeks is a good interval elseif($difference >= 60*60*24*7 && $difference < 60*60*24*30) { $interval = "ww"; } // If difference is between 30 days and 365 days // months is a good interval, again, the same thing // applies, if the 29th February happens to exist // between your 2 dates, the function will return // the 'incorrect' value for a day elseif($difference >= 60*60*24*30 && $difference < 60*60*24*365) { $interval = "m"; } // If difference is greater than or equal to 365 // days, return year. This will be incorrect if // for example, you call the function on the 28th April // 2008 passing in 29th April 2007. It will return // 1 year ago when in actual fact (yawn!) not quite // a year has gone by elseif($difference >= 60*60*24*365) { $interval = "y"; } // Based on the interval, determine the // number of units between the two dates // From this point on, you would be hard // pushed telling the difference between // this function and DateDiff. If the $datediff // returned is 1, be sure to return the singular // of the unit, e.g. 'day' rather 'days' switch($interval) { case "m": $months_difference = floor($difference / 60 / 60 / 24 / 29); 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++; } $datediff = $months_difference; // We need this in here because it is possible // to have an 'm' interval and a months // difference of 12 because we are using 29 days // in a month if($datediff==12) { $datediff--; } $res = ($datediff==1) ? "$datediff month ago" : "$datediff months ago"; break; case "y": $datediff = floor($difference / 60 / 60 / 24 / 365); $res = ($datediff==1) ? "$datediff year ago" : "$datediff years ago"; break; case "d": $datediff = floor($difference / 60 / 60 / 24); $res = ($datediff==1) ? "$datediff day ago" : "$datediff days ago"; break; case "ww": $datediff = floor($difference / 60 / 60 / 24 / 7); $res = ($datediff==1) ? "$datediff week ago" : "$datediff weeks ago"; break; case "h": $datediff = floor($difference / 60 / 60); $res = ($datediff==1) ? "$datediff hour ago" : "$datediff hours ago"; break; case "n": $datediff = floor($difference / 60); $res = ($datediff==1) ? "$datediff minute ago" : "$datediff minutes ago"; break; case "s": $datediff = $difference; $res = ($datediff==1) ? "$datediff second ago" : "$datediff seconds ago"; break; } return $res; } Thanks for help!! Quote Link to comment https://forums.phpfreaks.com/topic/208776-problem-with-function-for-getting-time-difference/ Share on other sites More sharing options...
PFMaBiSmAd Posted July 24, 2010 Share Posted July 24, 2010 The function name you are using in the function call is not the same name as your function definition and if you get rid of the @ in front of it, you would probably get an error that would point out the mismatch in the names. Don't ever put an @ in your code. They don't fix problems, they hide problems. There is no need for @ in code. On a live server display_errors should be OFF and on a development system display_errors should be ON. Quote Link to comment https://forums.phpfreaks.com/topic/208776-problem-with-function-for-getting-time-difference/#findComment-1090664 Share on other sites More sharing options...
defroster Posted July 24, 2010 Author Share Posted July 24, 2010 Ok, thanks a lot for your help. Really appreciate it. I did not know that '@' meant those things. Beginner at this. Now I get error: Fatal error: Call to undefined function get_TimeAgo() in /Applications/XAMPP/xamppfiles/htdocs/index.php on line 157 and line 157 is: $pasttime=get_TimeAgo($row['dateposted'],$dateto=-1); How do I call for the function then? Quote Link to comment https://forums.phpfreaks.com/topic/208776-problem-with-function-for-getting-time-difference/#findComment-1090666 Share on other sites More sharing options...
PFMaBiSmAd Posted July 24, 2010 Share Posted July 24, 2010 Someone already told you what the problem is - The function name you are using in the function call is not the same name as your function definition ... The solution would be to use the same name in the function call as in the function definition. Quote Link to comment https://forums.phpfreaks.com/topic/208776-problem-with-function-for-getting-time-difference/#findComment-1090668 Share on other sites More sharing options...
defroster Posted July 24, 2010 Author Share Posted July 24, 2010 Ahh thanks. I thought I had to get the function with code telling to get in some way. But now I changed it to: $pasttime=TimeAgo($row['dateposted'],$dateto=-1); Thanks for help! problem solved Quote Link to comment https://forums.phpfreaks.com/topic/208776-problem-with-function-for-getting-time-difference/#findComment-1090671 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.