Jump to content

auction/ebay style timer using PHP/MYSQL?


crf1121359

Recommended Posts

Hi guys,

 

I need to create a simple timer using PHP/MySQL, something similar to the eBay timer, but without JavaScript. I know eBay uses JavaScript for the timer to start ticking (without page refresh). I don't need this bit. I just need it to show the day/minutes left for "1" item only.

I searched on Google and found this code:

 

<?php



//list fields and convert to seconds

$countdown['days']=(1) * 24 * 60 * 60;

$countdown['hours']=(1) * 60 * 60;

// etc, etc

$countsum=time() + $countdown['days'] + $countdown['hours']; //and so on

// the above would be the timestamp to enter into the table



##########



// 'dumbed down' query
include "config/connect_to_mysql.php";

$result=mysql_query("SELECT * FROM tomProduct ORDER BY id DESC LIMIT 1;");



while ($row=mysql_fetch_assoc($result))

    $time=$row['date_added'] - time(); //this field would be a PHP timestamp (time())



$count=getdate($time);



$x=getdate(); //todays information



$count['mday'] -= $x['mday'];

$count['hour'] -= $x['mday'];

$count['minutes'] -= $x['minutes'];



echo "$count[mday] Days $count[hour] Hours $count[minutes] Minutes"; //etc



// untested, but should work

?>

I edited and connected it to my own MySQL database to see how it works, and on the page it shows the time in this format : -19 Days -26 Hours -3 Minutes.

If I refresh the page every 1 minute, the minutes will increase by 1. So, basically it doesn't count down; it actually counts up.

I need this to show how many days/minutes are left for that particular product,

and if the user refreshes the page, then the time will change if need be as it does in that script above.

Currently the timestamp for the item (when it was posted) gets stored in the MySQL database as "date" and the column called date_added.

Could anyone point me in the right direction please?

cheers

Link to comment
Share on other sites

Try this:

 

<?PHP

  $timeDiff = $row['date_added']-$_SERVER['REQUEST_TIME'];
    
  if($timeDiff <= 0) {
    $output = 'Timer Has Elapsed.';
  } else {
      
    $output = 'Time Remaining: ';
 
    $hours = floor($timeDiff/60/60);
    $mins  = floor($timeDiff/60)-($hours*60*60);
    $secs  = ($timeDiff-($hours*60*60))-($mins*60);

    //### Add hours to output
    $output .= $hours < 10 ? '0'.$hours.':' : $hours.':' ;
 
    //### Add minutes to output
    $output .= $mins < 10 ? '0'.$mins.':' : $mins.':' ;
 
    //### Add seconds to output
    $output .= $secs < 10 ? '0'.$secs : $secs ;
  }
 
  echo $output;
 
?>
Edited by PaulRyan
Link to comment
Share on other sites

Here is an old function of mine.  It tends to drop an hour under certain circumstances, although I never wasted time trying to track it down.  This function attempts to track leap years also.  Now, there may be a more current way of doing it easier, but this function has already been written.  You are welcome to use it.

 

 

 
<?php
 
//set real dates for start and end, otherwise *nix the strtotime() lines.
//$return 'days' will return days/hours/minutes/seconds.
//$return 'hours' will return hours/minutes/seconds.
//$return 'minutes' will return minutes/seconds.
//$return 'seconds' will return seconds.
function timeDifference($start,$end,$return='decade') {
$ex = explode('-',$start);
$sm = ($ex[1] < 10) ? (int)substr($ex[1],1,1) : (int)$ex[1];
$sy = (int)$ex[0];
$en = explode('-',$end);
$em = ($ex[1] < 10) ? (int)substr($ex[1],1,1) : (int)$ex[1];
$ey = (int)$ex[0];
 
$months = array(1 => 2678400, 2=>2419200, 3=>2678400, 4=>2592000, 5=>2678400, 6=>2592000, 7=>2678400,
8=>2678400, 9=>2592000, 10=>2678400, 11=>2592000, 12=>2678400);
$lyears = array(2000,2004,2008,2012,2016,2020,2024,2028,2032,2036,2040,
2044,2048,2052,2056,2060,2064,2068,2072,2076,2080,2084,
2088,2092,2096);
 
$start = strtotime($start);
$end = strtotime($end);
$difference = max($end, $start) - min($end,$start); 
    $time = NULL;
    //calculate time difference.
   switch($return) {
case 'decade':
$dy = $sy;
$decade = NULL;
for($i = 1; $i <= 10; $i++) {
@$dyear += (in_array($dy,$lyears)) ? 31622400 : 31536000;
++$dy;
}
   $decade = floor($difference/$dyear); //echo '<br/>' . $difference . '/' . $dyear . '=';
                $difference = $difference % $dyear; //echo $difference;
 
                    $time['decade'] = $decade;
case 'year':
$ty = $sy + 1;
$tm = $sm;
$year = NULL;
while($difference >= 31536000) {
@$tyear = (in_array($ty,$lyears)) ? 31622400 : 31536000;
if($difference >= $tyear) {
@$year++; //echo '<br/>' . $ty . ' = ' . $tyear;
$difference = $difference - $tyear;
}
++$ty;
} 
                    $time['year'] = $year;
case 'month': 
$month = NULL;
// $m = ($m == 12) ? 1 : ++$m;
while($difference > 2419200 || (!in_array($sy,$lyears) && $sm == 2 && $difference == 2419200)) {
if(in_array($sy,$lyears, true) && $sm === 2) {
@$month += ($difference >= 2505600) ? 1 : 0; //echo '<br/>' .$sm . '-' . $sy . '=2505600';
$difference = ($difference >= 2505600) ? $difference - 2505600 : $difference; 
}
else {
@$month += ($difference >= $months[$sm]) ? 1 : 0; //echo '<br/>' . $sm . '-' . $sy . '=' . $months[$sm];
$difference = ($difference >= $months[$sm]) ? $difference - $months[$sm] : $difference; 
}
$sy = ($sm == 12) ? ++$sy : $sy;
$sm = ($sm == 12) ? 1 : ++$sm;
}
//echo '<br />' . $difference;
                    $time['month'] = $month; 
case 'week':
$week = floor($difference/604800);
                $difference = $difference % 604800;
                    $time['week'] = $week;
case 'days':
$days = floor($difference/86400);
                $difference = $difference % 86400;
                    $time['day'] = $days;
case 'hours':
$hours = floor($difference/3600);
                $difference = $difference % 3600;
                    $time['hour'] = $hours;
case 'minutes':
$minutes = floor($difference/60);
                $difference = $difference % 60;
                    $time['minute'] = $minutes;
case 'seconds':
       $seconds = $difference;
                $time['second'] = $seconds;
    }   
    
       if(is_array($time)) {
if($start > $end) {
foreach($time as $key => $value) {
if($value > 0) {
if($value > 1) {
$key = $key . 's';
}
return $value . ' ' . $key . ' ';
}
}
}
elseif($end > $start) {
foreach($time as $key => $value) {
if($value > 0) {
if($value > 1) {
$key = $key . 's';
}
@$output .= $value . ' ' . $key . ' ';
}
}
}
        }
    return $output;
}  
 
Link to comment
Share on other sites

 

Try this:

 

<?PHP

  $timeDiff = $row['date_added']-$_SERVER['REQUEST_TIME'];
    
  if($timeDiff <= 0) {
    $output = 'Timer Has Elapsed.';
  } else {
      
    $output = 'Time Remaining: ';
 
    $hours = floor($timeDiff/60/60);
    $mins  = floor($timeDiff/60)-($hours*60*60);
    $secs  = ($timeDiff-($hours*60*60))-($mins*60);

    //### Add hours to output
    $output .= $hours < 10 ? '0'.$hours.':' : $hours.':' ;
 
    //### Add minutes to output
    $output .= $mins < 10 ? '0'.$mins.':' : $mins.':' ;
 
    //### Add seconds to output
    $output .= $secs < 10 ? '0'.$secs : $secs ;
  }
 
  echo $output;
 
?>

 

Thanks but what is this and how do you use it? I'm keep getting Timer Has Elapsed. on the page!!!

Edited by crf1121359
Link to comment
Share on other sites

I've changed it up a bit, and put it into a function:

 

 

<?PHP
    
  function timer($startTime) {
      
    $timeDiff = $startTime-$_SERVER['REQUEST_TIME'];
    
    if($timeDiff <= 0) {
      $output = 'Timer Has Elapsed.';
    } else {
      
      $output = 'Time Remaining: ';
 
      $days  = floor($timeDiff/60/60/24);
      $hours = floor(($timeDiff-($days*60*60*24))/60/60);
      $mins  = floor(($timeDiff-($days*60*60*24)-($hours*60*60))/60);
      $secs  = floor($timeDiff-($days*60*60*24)-($hours*60*60)-($mins*60));

      //### Add days to output
      $output .= $days != 1 ? $days.' days, ' : $days.' day, ' ;
    
      //### Add hours to output
      $output .= $hours != 1 ? $hours.' hours, ' : $hours.' hour, ' ;
 
      //### Add minutes to output
      $output .= $mins != 1 ? $mins.' mins, ' : $mins.' min, ' ;
 
      //### Add seconds to output
      $output .= $secs != 1 ? $secs.' secs' : $secs.' sec' ;
    }
   
    return $output;
  }
 
  include "config/connect_to_mysql.php";

  $result=mysql_query("SELECT * FROM tomProduct ORDER BY id DESC LIMIT 1;");
 
  while ($row=mysql_fetch_assoc($result)) {

    echo timer($row['date_added']). '<br> ';
 
  }
 
?>

 

$startTime will be the

Edited by PaulRyan
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.