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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.