0

I have this code for a countdown timer. its basically a combination of PHP/Javascript countdown timer which will gets the $end_date from a Mysql table/field.

The problem is that it will stop automatically (which is unwanted) at a certain time.

For example: I set the $end_date to September 19 2013 11:30:00 AM GMT in mysql database.

the countdown starts and works fine and starts counting down as it should. However, when the countdown timer reaches September 19 2013 13:00:00 PM GMT it will stop and it will show the Times Up message! Basically it will stop working or counting down once the $end_date has been changed to 13:00:00 PM.

I cannot see anything in my code that will cause this issue. apart from this line:

if ($now < $exp_date ) {
?>

but again, this line only tells the script when to start counting and as far i can see it shouldn't stop the countdown timer to stop as long as the timer has not reached the $end_date. or am I missing something?

here is my code:

<?php 
error_reporting(E_ALL);
ini_set('display_errors', '1');
?>
<?php date_default_timezone_set('GMT'); ?>
<?php
session_start();
// Run a select query to get my letest 6 items
// Connect to the MySQL database  
include "config/connect.php";
$dynamicList = "";
$sql = "SELECT * FROM item ORDER BY id";
$query = mysqli_query($db_conx, $sql);
$productCount = mysqli_num_rows($query); // count the output amount
if ($productCount > 0) {
    while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){ 
             $id = $row["id"];
             $product_name = $row["product_name"];
             $date_added = date("Y-m-d", strtotime($row["date_added"]));
             $end_date = date("F d Y H:i:s A T", strtotime($row["end_date"]));
             $price = $row["price"];
             $dynamicList .= '<div>' . $end_date . '
      </div>';
    }
} else {
    $dynamicList = "No Records";
}
?>

<?php
$date = $end_date;
$exp_date = strtotime($date);
$now = time();

if ($now < $exp_date ) {
?>
<script>
// Count down milliseconds = server_end - server_now = client_end - client_now
var server_end = <?php echo $exp_date; ?> * 1000;
var server_now = <?php echo time(); ?> * 1000;
var client_now = new Date().getTime();
var end = server_end - server_now + client_now; // this is the real end time

var _second = 1000;
var _minute = _second * 60;
var _hour = _minute * 60;
var _day = _hour *24
var timer;

function showRemaining()
{
    var now = new Date();
    var distance = end - now;
    if (distance < 0 ) {
       clearInterval( timer );
       document.getElementById('countdown').innerHTML = 'EXPIRED!';

       return;
    }
    var days = Math.floor(distance / _day);
    var hours = Math.floor( (distance % _day ) / _hour );
    var minutes = Math.floor( (distance % _hour) / _minute );
    var seconds = Math.floor( (distance % _minute) / _second );

    var countdown = document.getElementById('countdown');
    countdown.innerHTML = '';
   if (days) {
        countdown.innerHTML += 'Days: ' + days + '<br />';
    }
    countdown.innerHTML += 'Hours: ' + hours+ '<br />';
    countdown.innerHTML += 'Minutes: ' + minutes+ '<br />';
    countdown.innerHTML += 'Seconds: ' + seconds+ '<br />';
}

timer = setInterval(showRemaining, 1000);
</script>
<?php
} else {
    echo "Times Up";
}
?>
<div id="countdown"></div>

any help would be greatly appreciated.

8
  • 9
    There's no such time as 13 pm. Commented Sep 18, 2013 at 11:54
  • @Juhana, are you sure? because i echo the result on my page and i get this echo-ed there: September 19 2013 13:00:00 PM GMT ... where did you get that idea from that there is no such time as 13 PM? Commented Sep 18, 2013 at 12:02
  • What exactly are you echoing? What time do you expect 13 pm to be? My clock has only 12 hours. Commented Sep 18, 2013 at 12:09
  • @Juhana, i am echoing <?php echo $end_date; ?>. Commented Sep 18, 2013 at 12:10
  • 1
    H is the signifier for hours in a 24-hour clock. You need h if you use 12-hour clock. Commented Sep 18, 2013 at 12:13

1 Answer 1

2

13:00:00 PM is not a valid time. AM and PM are used to indicate which side of the 12-hour cycle the time is. You cannot logically say you're on the 13th hour of the 12 hour side of the clock.

EDIT: For clarity: 13:00 == 1 PM, 13:00 PM == nothing.

Sign up to request clarification or add additional context in comments.

4 Comments

so how come my PC is showing my time as 13:15 now? this doesn't make sense.
That is right. But it has no PM or AM on the end, this is the major problem. Please read my answer carefully.
And to say 13 PM would mean you were saying 13 hours after midday, which means it'd be referring to 1 AM or 01:00 and not 1 PM or 13:00.
OMG, mate, I can't believe it was only that. I can't believe I didn't know that. I just removed the H and it started working again! I appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.