Ash
Ash

Reputation: 444

making a simple countdown clock in php with variable end time

trying to make a simple countdown timer in PHP based on some input from a database which will tweak the end time slightly (with a constant base). However it seems to keep throwing up really strange numbers in the countdowns. I pretty much guarantee my math is wrong somewhere.

PHP

$timeUntil = 10; // for tweaking time remaining (days), this will be dynamic in actual program but will always be an integer
$timeStamp = 1445438099; // timestamp of roughly 2 months in the future
    $zUnixDays = $timeUntil * 86400; // converting integer (days) into seconds
    $zFinalTime = $timeStamp - $zUnixDays; //tweaking the constant time with the variable time
    $remaining = $zFinalTime - time(); // seconds remaining from the future time til now 
    $seconds_remaining = $remaining%60;
    $minutes_remaining = floor(($remaining%3600)/60);
    $hours_remaining = floor(($remaining%86400)/3600);
    $days_remaining = floor(($remaining%2592000)/86400);
    $zTimeCombined = array($days_remaining, $hours_remaining, $minutes_remaining, $seconds_remaining);
    echo json_encode($zTimeCombined);

JS

var result = JSON.parse(results);
                var zDays = result[0];
                var zHours = result[1];
                var zMinutes = result[2];
                var zSeconds = result[3];

this should return around 50 days remaining (2 months - 10 days.. very rough) but instead returns 21 days. Any ideas anyone?

Upvotes: 1

Views: 1760

Answers (3)

Mihai Matei
Mihai Matei

Reputation: 24276

I recommend you to use the DateTime and DateInterval objects

$dateTime = new DateTime();
$dateTime->setTimestamp(1445438099);

$timeInTheFuture = $dateTime->modify('-10 days');

$dateInterval = $timeInTheFuture->diff(new DateTime());

echo json_encode(explode(' ', $dateInterval->format('%d %h %i %s')));

Upvotes: 1

Marc B
Marc B

Reputation: 360682

Use DateTime instead, it's much easier:

$future = new DateTime();
$future->setTimestamp(1445438099); // passing in to constructor directly is wonky
$start = $future->sub(new DateInterval('P10D'));
$diff = $start->diff(new DateTime());
$interval = $diff->format('%y:%m:%d:%h:%i:%s');
// 0:1:20:23:29:45
list($year, $month, $day, $hour, $minute, $second) = explode(':', $interval);

Upvotes: 1

Henry
Henry

Reputation: 607

<?php
$daysToFuture = 10;

// 10 Days in Future
$futureTs = mktime(0,0,0,date("n"),date("j")+$daysToFuture,date("Y"));

// remaining till now ...
$remaining = $futureTs-time();

// in Days
$days_remaining = date("d",$remaining);
echo "<pre>"; print_r($days_remaining); echo "</pre>";

// in hours
$hours_remaining = date("H",$remaining);
echo "<pre>"; print_r($hours_remaining); echo "</pre>";

// in minutes
$minutes_remaining = date("i",$remaining);
echo "<pre>"; print_r($minutes_remaining); echo "</pre>";

// in seconds
$seconds_remaining = date("s",$remaining);
echo "<pre>"; print_r($seconds_remaining); echo "</pre>";

// Array
$zTimeCombined = array($days_remaining, $hours_remaining, $minutes_remaining, $seconds_remaining);
echo "<pre>"; print_r($zTimeCombined); echo "</pre>";

Upvotes: 1

Related Questions