0

I am trying to make a countdown timer which displays how many days until the product expires, and when there are 0 days, I want to display a message saying that it has expired:

<?php
$target =  mktime(0, 0, 0, 1, 7, 2013) ;
$today = time () ;
$difference =($target-$today) ;
$days =(int) ($difference/86400) ;
if ($today == $target)
{
print "this product has expired";
}
else
{
print "This product expires in $days days";
}
?>

When you visit the page, it always displays the second message, even though today is the same date as the target date.

If possible as well, could you tell me how to display the minutes and seconds without reloading the page? I would also like to update a MySQL Database when there are 0 days/minutes left, is this possible? There does not need to be a start/stop button.

3
  • Look at the right hand column below the ads. Commented Jan 6, 2013 at 21:37
  • try strtotime("2013-07-01"); instead of mktime Commented Jan 6, 2013 at 21:39
  • Use strcmp() to compare two strings Commented Jan 6, 2013 at 21:42

2 Answers 2

1

I think you already got the solution. But if someone is still looking for this solution then this might help. In this solution we can split days,hours,mins,seconds.

Reverse timer from javascript:

var target_date = new Date('Aug 01 2014 20:47:00').getTime();
    var days, hours, minutes, seconds;
    var countdown = document.getElementById('timeremaining');
    var countdownTimer = setInterval(function () {
        var current_date = new Date().getTime();
        var seconds_left = (target_date - current_date) / 1000;

        days = parseInt(seconds_left / 86400);
        seconds_left = seconds_left % 86400;

        hours = parseInt(seconds_left / 3600);
        seconds_left = seconds_left % 3600;
        minutes = parseInt(seconds_left / 60);
        seconds = parseInt(seconds_left % 60);

        if(days <= 0 && hours <= 0 && minutes <= 0 && seconds <= 0 )
        {
           document.getElementById('timeremaining').innerHTML = '';
           clearInterval(countdownTimer);              
        }
        else
        {       
            if(days>0)
              {
                 days= days+'d,';
              } 
              else
              {
                 days='';
              }            
            countdown.innerHTML ='( ' + days + checkTime(hours) + ':'+ checkTime(minutes) + ':' + checkTime(seconds) +' remaining)';    
        }
    }, 1000);

    function checkTime(i) {
        if (i < 10) {i = '0' + i};  
        return i;
    }      
Sign up to request clarification or add additional context in comments.

Comments

1

There's two issues here. The first is that $today and $target are both measured in seconds.

This means unless you visit the page at the exact same second as target you wont see the message.

This also means that any time past the expiry will also work which presumably is not what you want. Try using:

if($today > $target)

To display the minutes and seconds you should look into javascript which could countdown the time.

Comments

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.