sillyman Posted March 31, 2010 Share Posted March 31, 2010 Have a look at below script, any ideas would be appreciated. It all works together if not within the MySQL Fetch Array. <?php include 'connectdb.php'; include 'time_compare.php'; include 'time.php'; while($row = mysql_fetch_array($query2)) { $end = $row['end']; echo time_compare($end); echo "<br />"; $id = $row['itemID']; echo $row['itemID']; echo "<br />"; echo "Start Time ".$row['start']; echo "<br />"; echo "End Time ".$row['end']; echo "<br />"; echo "<p />"; } ?> <?php function time_compare($end) { $today = date(U); $today = strtotime($today); $expiry = strtotime($end); if ($expiry > $today) { $difference =($expiry-$today) ; echo time_format($difference); } else { echo "Item Sold"; } } ?> <?php function time_format($difference) { if (($difference /86400) > 10) { $print = floor($difference /86400) . ' days '; return $print; } else if (($difference /86400) < 10 && ($difference /86400) > 3) { $print = floor($difference /86400) . ' days '. floor(($difference % 86400)/3600). ' hours '; return $print; } else if (($difference /86400) < 3 && ($difference /86400) > 1) { $print = floor($difference /86400) . ' days '. floor(($difference % 86400)/3600). ' hours '. floor((($difference % 86400)%3600)/60) . ' minutes '; return $print; } else if (($difference /86400) < 1) { if (((($difference % 86400)%3600)/60) > 60) { $print = floor(($difference % 86400)/3600). ' hours '. floor((($difference % 86400)%3600)/60) . ' minutes '; return $print; } else if (((($difference % 86400)%3600)/60) < 60 && ((($difference % 86400)%3600)/60) > 1) { $print = floor((($difference % 86400)%3600)/60) . ' minutes '. floor(((($difference % 86400)%3600)%60)) . ' seconds.'; return $print; } else { $print = floor(((($difference % 86400)%3600)%60)) . ' seconds.'; return $print; } } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/197083-function-call-within-a-function-call-within-mysql-fetch-array-returns-wrong-res/ Share on other sites More sharing options...
ignace Posted March 31, 2010 Share Posted March 31, 2010 If you use the built-in DateTime & DateInterval classes you could just use: while ($row = mysql_fetch_assoc($result)) { $start = new DateTime($row['start']); $end = new DateTime($row['end']); $interval = $end->diff($start); // replaces time_compare() if ($interval->invert) { echo 'Item Sold'; continue; } // these 4 lines replace your entire time_format() function with equal functionality echo (!empty($interval->d) ? $interval->d . ' days ' : ''), (!empty($interval->h) ? $interval->h . ' hours ' : ''), (!empty($interval->i) ? $interval->i . ' minutes ' : ''), (!empty($interval->s) ? $interval->s . ' seconds ' : ''); } If you extend both the DateTime & DateInterval class it can be as simple as: while ($row = mysql_fetch_assoc($result)) { $start = new MyDateTime($row['start']); $end = new MyDateTime($row['end']); $interval = $end->diff($start); // replaces time_compare() if ($interval->isSold()) { echo 'Item Sold'; continue; } // these 4 lines replace your entire time_format() function with equal functionality echo $interval; } Quote Link to comment https://forums.phpfreaks.com/topic/197083-function-call-within-a-function-call-within-mysql-fetch-array-returns-wrong-res/#findComment-1034562 Share on other sites More sharing options...
sillyman Posted March 31, 2010 Author Share Posted March 31, 2010 Thankyou. But I need to have them as separate functions to call upon. And I'm comparing not start date and end date but today's date and end date. I do get a variable passed back to echo but its not right "14994 days". I think their is something wrong with my variable scope or where I am placing my include statements. Quote Link to comment https://forums.phpfreaks.com/topic/197083-function-call-within-a-function-call-within-mysql-fetch-array-returns-wrong-res/#findComment-1034616 Share on other sites More sharing options...
ignace Posted March 31, 2010 Share Posted March 31, 2010 notice 'now' while ($row = mysql_fetch_assoc($result)) { $start = new DateTime('now'); $end = new DateTime($row['end']); $interval = $end->diff($start); // replaces time_compare() if ($interval->invert) { echo 'Item Sold'; continue; } // these 4 lines replace your entire time_format() function with equal functionality echo (!empty($interval->d) ? $interval->d . ' days ' : ''), (!empty($interval->h) ? $interval->h . ' hours ' : ''), (!empty($interval->i) ? $interval->i . ' minutes ' : ''), (!empty($interval->s) ? $interval->s . ' seconds ' : ''); } Quote Link to comment https://forums.phpfreaks.com/topic/197083-function-call-within-a-function-call-within-mysql-fetch-array-returns-wrong-res/#findComment-1034630 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.