I'm having some problem with php not counting down till an expire message. I'm trying to create a promotional script which will run based on data set, it may be one day promotion like say Saturday special or few days promotion. The other problem I'm facing is lack of javascript/jquery knowledge to make a basic countdown with ajax instead of static php echo which is updated only on refresh.
Well here is the code that is responsible for the echoing of deal time remaining and a message if expired. Here is the file which is included in the index page to build the deal bar html with php.
$now = new DateTime();
$ends = new DateTime('Mar 22, 2015, 11:00 am');
$left = $now->diff($ends);
if($now != $ends) {
$promo_d = $left->format('%a'); #days
$promo_h = $left->format('%h'); #hours
$promo_m = $left->format('%i'); #min
$promo_s = $left->format('%s'); #sec
$deal = "Expires in: ".$promo_h." hours ".$promo_m." min ".$promo_s." sec";
} else {
$deal = "Expired!";
}
$html = <<<DEALBAR
<div class="row-fluid">
<div class="col-xs-12 col-sm-12 col-md-12 col-lg-12">
<br>
<div class="offer offer-success">
<div class="shape">
<div class="shape-text">
<i class="fa fa-scissors fa-2x"></i>
</div>
</div>
<div class="offer-content">
<h2 class="lead">
<strong>Saturday Special !!!</strong> <span class="pull-right"><i class="fa fa-clock-o"></i> ($deal)</span>
</h2>
<hr>
<p style="font-size:16px">
This is a one day Saturday Special active till March 22, 2015 9:00AM. Simply quote <span class="label label-success">SAT 40</span> during your service call to resive $40 dollars off your work order.
</p>
</div>
</div>
<br>
</div>
</div>
DEALBAR;
echo $html;
and here is the jquery I used for an older countdown timer, but I sadly have no clue how to make it work with above code nicely :(
function startTimer(duration, display) {
var timer = duration, minutes, seconds;
setInterval(function () {
minutes = parseInt(timer / 60, 10)
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.text(minutes + ":" + seconds);
if (--timer < 0) {
timer = duration;
}
}, 1000);
}
jQuery(function ($) {
var fiveMinutes = 60 * 28,
display = $('.deal-countdown');
startTimer(fiveMinutes, display);
});
Can some one explain why my script is not echoing "Expired!" and instead keeps going and how would I be able to make the jquery work with the above script? Instead of updating data on page refresh?
Thanks in advance
if($now != $ends) {
will work only for that very second when they both match, one second after that it will again go wrong. You need
if($now <= $ends) {
This will it will create your time string only if now
is not beyond ends