yeah its me
yeah its me

Reputation: 1141

How to do this countdown

I have a end date in the database

i get the current date, let's suppose is in UTC

class Datecalc
{

    public $year;
    public $month;
    public $day;
    public $hour;
    public $min;
    public $sec;

    function diff($start,$end = false)
    {
        if($start > $end)
        {
            $this->day = 0;
            $this->hour = "00";
            $this->min = "00";
            $this->sec = "00";
            return false;

        }
        if(!$end) { $end = time(); }
        if(!is_numeric($start) || !is_numeric($end)) { return false; }
        $start  = date('Y-m-d H:i:s',$start);
        $end    = date('Y-m-d H:i:s',$end);
        $d_start    = new DateTime($start);
        $d_end      = new DateTime($end);
        $diff = $d_start->diff($d_end);

        $this->year    = $diff->format('%y');
        $this->month    = $diff->format('%m');
        $this->day      = $diff->format('%d');
        $this->hour     = $diff->format('%H');
        $this->min      = $diff->format('%I');
        $this->sec      = $diff->format('%S');

        return true;
    }

}

I use this function to calculate the difference in time, but the problem is that i cant count days to 99, and when the date is 00:00:00, the days -1, and its sets to 98 and the time 23:59:59, in this code its all good, but if the number of days gets higer than 30, its resets to 01, i think you understand what i am trying to say, please help!!

in other words i need to count the days separately, and the time needs to be binded to that days

Upvotes: 1

Views: 126

Answers (2)

Brent Baisley
Brent Baisley

Reputation: 12721

First, you should be checking if $end is false before you check is $start is greater than $end.

That said, the diff method returns a DateInterval object (http://us.php.net/manual/en/class.dateinterval.php), which has a number of options, including total number of days between the dates, as well as years, months, days, etc. You probably want to be using what is already returned by the diff method, rather than the format function.

$diff = $d_start->diff($d_end);
echo 'Total days difference: '.$diff->days;
echo 'Year, Month, Days, time difference: '.
$diff->y.' y, '.$diff->m.' m, '.$diff->d.' d, '.$diff->h.' h, '.$diff->i.' m, '.$diff->s.' s, ';

Upvotes: 1

Daniel Gruszczyk
Daniel Gruszczyk

Reputation: 5622

The method you use will never allow you to get days > 30 because any days above 30 will be converted to another month...

This might be better approach for you:
http://www.prettyscripts.com/code/php/php-date-difference-in-days
Or
http://www.bizinfosys.com/php/date-difference.html
(Simple google search btw...)

Upvotes: 1

Related Questions