Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.