PHP Countdown timer for promotion using strtotime("future date/time") - strtotime("now") = countdown?

SSpoke

I'm trying to setup a synchronized promotion countdown timer which will keep counting down using JavaScript.

But the start time should always be retrieved using PHP everytime you refresh page.

Everytime I refresh the page wait the time is always the same, even though maybe 30 seconds already passed.

Pretty much the $left isn't working properly

<?php
     $promotionEnds = 'May 31, 2014, 11:59 pm'; //Enter date for promotion
     $left = date("Y-m-d h:i:s", strtotime($promotionEnds, strtotime("now")));
     echo "Time = ". strtotime($promotionEnds)."\r\n";
     echo "Now Time = ". strtotime("now")."\r\n";
     echo "Countdown = ".$left."\r\n";
?>

Also can I make it go into at Y = 0 m = 0 d = 0 h = 0 i = 0 s = 0?

Or do I have to work with the unix timestamp seconds

Here is some outputs

Time = 1401566340
Now Time = 1401234087
Countdown = 2014-05-31 11:59:00

Time = 1401566340
Now Time = 1401234098
Countdown = 2014-05-31 11:59:00

Time = 1401566340
Now Time = 1401234111
Countdown = 2014-05-31 11:59:00

Here is something new I tried.

<?php
    $endPromotionSeconds = strtotime($promotionEnds);

    $days    = floor((time() - $endPromotionSeconds) / 86400);
    $hours   = floor((time() - $endPromotionSeconds) / 3600);
    $minutes = ((time() - $endPromotionSeconds) / 60) % 60;
    $seconds = (time() - $endPromotionSeconds) % 60;

    echo "Countdown = d " . $days . " h " . $hours . " m " . $minutes . " s " . $seconds . "\r\n";
?>

Countdown = d -3.835625 h -3.835625 m -3.835625 s -3.835625
Countdown = d -3.8354166666667 h -3.8354166666667 m -3.8354166666667 s -3.8354166666667 

In the end the HTML should generate

<dd>
    <div class="timer_wrp">Remaining
        <ul class="timer countdown">
            <li><i><?php echo $daysLeft; ?></i>Day</li>
            <li><i><?php echo $hoursLeft; ?></i>Hour</li>
            <li><i><?php echo $minutesLeft; ?></i>Min</li>
            <li><i><?php echo $secondsLeft; ?></i>Sec</li>
        </ul>
    </div>
</dd>
John Conde

It doesn't look like you actually find the difference between now and the end of the promotion to get the time remaining.

 $now  = new DateTime();
 $ends = new DateTime('May 31, 2014, 11:59 pm'); 
 $left = $now->diff($ends);

<dd>
    <div class="timer_wrp">Remaining
        <ul class="timer countdown">
            <li><i><?php echo $left->format('%a'); ?></i>Day</li>
            <li><i><?php echo $left->format('%h'); ?></i>Hour</li>
            <li><i><?php echo $left->format('%i'); ?></i>Min</li>
            <li><i><?php echo $left->format('%s'); ?></i>Sec</li>
        </ul>
    </div>
</dd>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related