2017-09-11 2 views
0

i bin neu in php, auch neu zu Stackoverflow und bei weitem es ist schon ein paar Monate i PHP begann zu lernen,PHP: macht einen Countdown Auto-Aktualisierung

so mein Punkt macht den Countdown auto- Refresh, derzeit muss ich die Seite aktualisieren das neue Ergebnis zu erhalten, ist es eine Möglichkeit, es auto-aktualisierbare

i die code zu folgenden verwendet, um zu erreichen, was ich brauche:

<?php 

$date = strtotime("September 12, 2017 2:00 PM"); 
$remaining = $date - time(); 


$days_remaining = floor($remaining/86400); 
$hours_remaining = floor(($remaining % 86400)/3600); 
$min_remaining = floor(($remaining % 3600)/60); 
$seconds_remaining = floor($remaining % 60); 

echo "There are $days_remaining days and $hours_remaining hours left and 
$min_remaining minutes left and $seconds_remaining left"; 

?> 

aber .. ich muss die Seite jedes Mal für neues Ergebnis aktualisieren.

Update:

Was zum Teufel .. ich hatte Javascript zu verwenden. wenn Sie möchten, verwenden Sie diesen Code es funktioniert perfekt

<script> 
    var end = new Date('09/12/2017 10:1 AM'); 

    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); 

     document.getElementById('countdown').innerHTML = days + 'days '; 
     document.getElementById('countdown').innerHTML += hours + 'hrs '; 
     document.getElementById('countdown').innerHTML += minutes + 'mins '; 
     document.getElementById('countdown').innerHTML += seconds + 'secs'; 
    } 

    timer = setInterval(showRemaining, 1000); 
</script> 

<!DOCTYPE html> 
<head> 

</head> 
<body> 
<div id="countdown"> 

    <h1 id="countdown"></h1> 



</div> 
    </body> 
+0

Sie würden AJAX verwenden. –

+0

@JayBlanchard, nur Option? –

+1

So funktioniert php. Verwenden Sie JavaScript, wenn Sie einen Live-Countdown wünschen – Andreas

Antwort

0

Hier ist ein weiteres Beispiel für einen Timer für Sie, dieser enthält seine Methoden und legt nur einige seiner Methoden für den externen Bereich offen.

// Setup countdown timer. 
const Timer = (countdownTime, elementId, intervalTime = 1000, doneMessage = 'Expired.') => { 
    // Set up an internal datastore, this is not accessible from outside the method. 
    let store = { 
    end: new Date(countdownTime).getTime(), 
    current: null, 
    interval: intervalTime, 
    intervalId: null, 
    element: document.getElementById(elementId), 
    msg: doneMessage, 
    }; 

    // Format time and update attached element. 
    const updateTime =() => { 
    // Get current difference. 
    let now = store.end - store.current; 

    // If current remaining is greater than 0 format the timer. 
    if (now > 0) { 
     let d = Math.floor(now/(1000 * 60 * 60 * 24)) 
     , h = Math.floor((now % (1000 * 60 * 60 * 24))/(1000 * 60 * 60)) 
     , m = Math.floor((now % (1000 * 60 * 60))/(1000 * 60)) 
     , s = Math.floor((now % (1000 * 60))/1000); 

     store.element.textContent = `${d} days --- ${h} hours --- ${m} minutes --- ${s} seconds`; 
     return; 
    } 
    // use expired message if not. 
    store.element.textContent = store.msg; 
    }; 

    // This is the method your interval is using to tick down the time. 
    const tick =() => { 
    // get current time. 
    store.current = new Date().getTime(); 
    // stop Timer interval if time is negative. 
    if (store.current < 0) { 
     public.stop(); 
    } 
    updateTime(); 
    } 

    // these methods will be accessible from outside the Timer function. 
    let public = { 
    // lets you see your datastore keys. 
    get: key => (store.hasOwnProperty(key)) ? store[key] : false, 
    // start interval. 
    start:() => { 
     store.intervalId = setInterval(tick, store.interval) 
    }, 
    // stop interval 
    stop:() => clearInterval(store.intervalId), 
    // clear and reset interval 
    reset:() => { 
     clearInterval(store.intervalId); 
     store.interval = null; 
    }, 
    } 
    // return public methods for your timer. 
    return public; 
} 

// instantiate timer with basic options. 
let countdown = Timer('09/12/2017 10:1 AM', 'countdown'); 
// start countdown. 
countdown.start(); 
0

sollten Sie JavaScript verwenden, um dies zu tun, auf diese link ein Beispiel hat, wie einen Countdown-Timer mit JavaScript zu implementieren.

+0

danke, schon wieder eins, nochmal danke für die Antwort –