Jump to content

Very Simple Countdown Script - Stumped !


chiprivers

Recommended Posts

<?php

$year = 2008;
if ( !$days = getDaysLeft($year) ) {
    echo 'The year entered has already passed!';
} else {
    echo 'Today is ' . date("M jS, Y") . '. There are ' . $days . ' days left until the end of ' . $year . '.';
}

function getDaysLeft($year) {
    if ( date("L", mktime(0, 0, 0, 1, 1, $year)) == 1 ) {
        $days = 366;
    } else {
        $days = 365;
    }
    if ( date("Y") < $year ) {
        $daysBehind = mktime(0, 0, 0, 1, 1, $year) - mktime(0, 0, 0, date("m"), date("d"), date("Y"));
        $additional = intval($daysBehind/86400);
        $days = $days + $additional;
    } elseif ( date("Y") > $year ) {
        return FALSE;
    } else {
        $days = $days - date("z");
    }
    return $days;
}
?>

 

PhREEEk

A shorter way..

 

 

<?php
$intYear = "2009";

$intTime = strtotime("{$intYear}-01-01");
$intToday = time();
$intDays = floor(($intTime-$intToday)/86400);
print $intDays;



?>

 

I ran your script, and from today's date, your script says 366 days left until 2009, while mine states 367. Being that 2008 is a leap year, hence having 366 days instead of 365, and adding in today being 1 day until 2008 begins, I would have to conclude that 367 is the correct result, no? Unless I've missed something...

 

I also created the function so that the OP can re-use it if he finds necessary, or someone else wanders in looking for a similar countdown function/script thingamajig.

 

PhREEEk

You just need to roundup, rather than round down. If you rerun rajiv's code with January 1st 2008 as the finish date, it will show 0 days, when, of course, it should be 1. By rounding down, you don't count today.

 

<?php
$intYear = "2009";

$intTime = strtotime("{$intYear}-01-01");
$intToday = time();
$intDays = ceil(($intTime-$intToday)/86400);
print $intDays;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.