<?php

include 'inc/db.php';

function dateDiff($start, $end) {

$start_ts = strtotime($start);

$end_ts = strtotime($end);

$diff = $end_ts - $start_ts;

return floor($diff / 86400);

}
$q ="SELECT * FROM date";
$p=mysql_query($q);
while ($res=mysql_fetch_array($p))
{
    $start =$res['sdate'];
}
$qc ="SELECT * FROM accounts";
$pc=mysql_query($qc);
while ($r=mysql_fetch_array($pc))
{
    $elecend=$r['elec_contract_edate']; 
    $gasend=$r['gas_contract_edate'];



       $elecshow =dateDiff( $start,$elecend );
       $gasshow =dateDiff( $start,$gasend );
       echo $elecshow."\tDays Remaining";
       echo $gasshow."\tDays Remaining";

}
?>

im getting this output "-15920 Days Remaining 0 Days Remaining8 Days Remaining11 Days Remaining "
i want to eliminate -15920 Days Remaining 0 Days kindly help me out

Member Avatar for Zagga

Do you mean you don't want to show just those2 specific values, or nothing below 0?

if ($elecshow > 0){
    echo $elecshow."\tDays Remaining";
}

What are the date values that yield unwanted results?

I am not sure what you want, but I'll suppose that you want to eliminate the first element. If yes, then add:

while($r=mysql_fetch_array($pc) && $i != 0){
....
}

or you can use the limitation in the query

if you want to eliminate the negative number and the zero, then:

while($r=mysql_fetch_array($pc) && dateDiff($r['elec_contract_edate']) > 0){
....
}

thanks bros problem solved by doing this

if ($elecshow > 0){
    echo $elecshow."\tDays Remaining";
}

Great! Click on "mark solved"

Member Avatar for Zagga

Depending on your situation, cmps' second idea of limiting the values in the query may be a better solution.

My example will still retrieve the 0 and negative values from the database. If you don't need to use these values then you don't need to waste resources by getting them with the query.

Member Avatar for diafol

I don't really follow this, but wouldn't something like this do?

SELECT gasdate, elecdate FROM utilities WHERE (FLOOR((gasdate - UNIX_TIMESTAMP())/86400) = 2) OR (FLOOR((elecdate - UNIX_TIMESTAMP())/86400) = 2)

This is assuming that there are 2 columns in the table - gasdate and elecdate
You then still have to check with php because you retrieve both fields even if only one value = 2.

I'd have a slightly different schema:

utility_bills

bill_id (PK)
user_id (FK)
utility_id (FK, e.g. 1 = electricity, 2 = gas)
enddate (int)

utility_types

utype_id (PK)
utility_type (varchar, e.g. electricity, gas)

$qualifier = '= 2'; // >2 or >=2 or you can use BETWEEN ... AND ... etc.

$q = "SELECT b.bill_id, t.utility_type, FROM_UNIXTIME(b.enddate, '%M %D, %Y') AS enddt, FLOOR((b.enddate - UNIX_TIMESTAMP())/86400) AS to_days FROM utility_bills AS b INNER JOIN utility_types AS t ON b.utility_id = t.utype_id HAVING to_days $qualifier"; 

You could order the results according to your needs, e.g. all electicity ones first etc, Just a thought. NOT TESTED.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.