Jump to content

Function Call within a Function Call within MySQL Fetch Array returns wrong res


sillyman

Recommended Posts

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;
	}
}
}
?>

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;
}

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.

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 ' : '');
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.