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";
while ($res=mysql_fetch_array($p))
    $start =$res['sdate'];
$qc ="SELECT * FROM accounts";
while ($r=mysql_fetch_array($pc))

       $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

4 Years
Discussion Span
Last Post by diafol

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

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

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

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.

Edited by Zagga


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:


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


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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.