usertest
usertest

Reputation: 27628

PHP Countdown to Date

How could set a date and get a countdown in PHP? For example if I set the date as 3 December 2PM it would tell me how many days and hours are remaining.

No need for user inputs for the date as it will be hard coded.

Thanks.

Upvotes: 18

Views: 80374

Answers (7)

WiMantis
WiMantis

Reputation: 396

For those looking for a function capable of handling larger and smaller time span (php >5.3) :

/**
 * Return a textual representation of the time left until specified date
 */
function timeleft(DateTime $date){
  $now = new DateTime();

  if($now > $date){
    return '0 second';
  }

  $interval = $date->diff($now);

  if($interval->y){
    return $interval->format("%y year").($interval->y > 1 ? 's':'');
  } else if($interval->m){
    return $interval->format("%m month").($interval->m > 1 ? 's':'');
  } else if($interval->d){
    return $interval->format("%d day").($interval->d > 1 ? 's':'');
  } else if($interval->h){
    return $interval->format("%h hour").($interval->h > 1 ? 's':'');
  } else if($interval->i){
    return $interval->format("%i minute").($interval->i > 1 ? 's':'');
  } else if($interval->s) {
    return $interval->format("%s second").($interval->s > 1 ? 's':'');
  } else {
    return 'milliseconds';
  }
}

Upvotes: 0

huzi8t9
huzi8t9

Reputation: 292

Using @Izhar Aazmi solution, you could set this up nicely for display, as such:

    public function countdown($time, $h = true, $m = true, $s = true) {
        $rem = $time - time();
        $day = floor($rem / 86400);
        $hr = floor(($rem % 86400) / 3600);
        $min = floor(($rem % 3600) / 60);
        $sec = ($rem % 60);

        if ( $day && !$h ) {
            if ( $hr > 12 ) $day++; // round up if not displaying hours
        }

        $ret = Array();
        if ( $day && $h ) $ret[] = ($day ? $day ." day".($day==1?"":"s") : "");
        if ( $day && !$h ) $ret[] = ($day ? $day . " day" . ($day == 1 ? "" : "s") : "");
        if ( $hr && $h ) $ret[] = ($hr ? $hr ." hour" . ($hr==1?"":"s") : "");
        if ( $min && $m && $h ) $ret[] = ($min ? $min ." minute". ($min==1?"":"s") : "");
        if ( $sec && $s && $m && $h ) $ret[] = ($sec ? $sec ." second".($sec==1?"":"s") : "");

        $last = end($ret);
        array_pop($ret);
        $string = join(", ", $ret)." and {$last}";

        return $string;
    }

I hope this helps! It's a nice clean way or displaying the countdown.

Upvotes: 1

JonnySchieds
JonnySchieds

Reputation: 1

Did this countdown until the end of the semester:

    $endOfSemester = mktime(15,30,0,5,21,2015);
    $now = time();
    $secondsRemaining = $endOfSemester - $now;

    define('SECONDS_PER_MINUTE', 60);
    define('SECONDS_PER_HOUR', 3600);
    define('SECONDS_PER_DAY', 86400);

    $daysRemaining = floor($secondsRemaining / SECONDS_PER_DAY); //days until end
    $secondsRemaining -= ($daysRemaining * SECONDS_PER_DAY); //update variable

    $hoursRemaining = floor($secondsRemaining / SECONDS_PER_HOUR); //hours until end
    $secondsRemaining -= ($hoursRemaining * SECONDS_PER_HOUR); //update variable

    $minutesRemaining = floor($secondsRemaining / SECONDS_PER_MINUTE); //minutes until end
    $secondsRemaining -= ($minutesRemaining * SECONDS_PER_MINUTE); //update variable

    echo("<h3>There are $daysRemaining days, $hoursRemaining hours, $minutesRemaining minutes, $secondsRemaining seconds until the end of the semester</h3>"); //print message

Upvotes: 0

Izhar Aazmi
Izhar Aazmi

Reputation: 935

Let me play around like this:

$rem = strtotime('2012-08-01 14:00:00') - time();
$day = floor($rem / 86400);
$hr  = floor(($rem % 86400) / 3600);
$min = floor(($rem % 3600) / 60);
$sec = ($rem % 60);
if($day) echo "$day Days ";
if($hr) echo "$hr Hours ";
if($min) echo "$min Minutes ";
if($sec) echo "$sec Seconds ";
echo "Remaining...";

Try this at your leisure... :-)

NOTE: There is no if() test for echo "Remaining...", just coz you wont process this in case when $rem <= 0. Isn't it?

Upvotes: 22

GZipp
GZipp

Reputation: 5416

PHP 5.3 allows this:

$dt_end = new DateTime('December 3, 2009 2:00 PM');
$remain = $dt_end->diff(new DateTime());
echo $remain->d . ' days and ' . $remain->h . ' hours';

Upvotes: 10

Kaspars Foigts
Kaspars Foigts

Reputation: 159

It's not as trivial as subtracting strtotime() results, since there are daylight savings and time would be mathematically correct, but not physically. Anyway, for these purposes you should use gmdate() function, which has no daylight savings:

$date = gmdate('U', strtotime('2009-12-03 14:00'));

// Get difference between both dates without DST
$diff = $date - gmdate('U');
// Days (in last day it will be zero)
$diff_days = floor($remaining / (24 * 60 * 60));
// Hours (in the last hour will be zero)
$diff_hours = floor($remaining % (24 * 60 * 60) / 3600);

Upvotes: 3

Travis
Travis

Reputation: 4078

You can use the strtotime function to get the time of the date specified, then use time to get the difference.

$date = strtotime("December 3, 2009 2:00 PM");
$remaining = $date - time();

$remaining will be the number of seconds remaining. Then you can divide that number to get the number of days, hours, minutes, etc.

$days_remaining = floor($remaining / 86400);
$hours_remaining = floor(($remaining % 86400) / 3600);
echo "There are $days_remaining days and $hours_remaining hours left";

Upvotes: 42

Related Questions