Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ilmala's avatar
Level 17

difference between two given dates without using PHP date functions or objects

i need to calculate difference between two given dates without using PHP date functions or objects here my first code but it dosn't work weel! Any idea/tips?

Thanks

protected function intiDatesFromSmallerToBigger()
    {
        $start = new DateSet($this->startDate);
        $end = new DateSet($this->endDate);

        if ($start->integer > $end->integer)
        {
            $this->fromDate = $end;
            $this->toDate = $start;
            $this->invert = true;
            return;
        }

        $this->fromDate = $start;
        $this->toDate = $end;
    }

    protected function isLeapYear($year)
    {
        return ((($year % 4) == 0) && ((($year % 100) != 0) || (($year %400) == 0)));
    }

    protected function calcDiffDays()
    {
        $increment = 0;
        if ($this->fromDate->day > $this->toDate->day)
        {
            $increment = $this->monthDay[(int)$this->fromDate->month - 1];
        }

        if ($increment == 28 && $this->isLeapYear($this->fromDate->year)){
            $increment = 29;
        }

        $this->days = $this->toDate->day;

        if($increment > 0) {
            $this->days += $increment;
            $this->months = 1;
        }

        if($this->fromDate->month < $this->toDate->month && $this->fromDate->day > $this->toDate->day) {
            $this->days--;
        }

        $this->days -= $this->fromDate->day;
    }

    private function calcDiffMonths()
    {
        $months = $this->toDate->month - ($this->fromDate->month + $this->months);
        if($this->fromDate->month + $this->months > $this->toDate->month) {
            $months += 12;
            $this->years = 1;
        }
        $this->months = $months;
    }

    protected function calcDiffYears()
    {
        $years = $this->toDate->year - ($this->fromDate->year + $this->years);
        $this->years = $years;
    }

    protected function calcDiffTotalDays()
    {
        $total = $this->days;

        $days=0;
        foreach(range($this->fromDate->year,$this->toDate->year) as $year) {
            foreach($this->monthDay as $index=>$monthDays) {
                $month = $index +1;
                if($year == $this->fromDate->year and $month < $this->fromDate->month) {
                    continue;
                }
                if($monthDays == 28 and $this->isLeapYear($year)) {
                    $days++;
                }
                $days += $monthDays;

                if($year == $this->toDate->year and $month > $this->toDate->month) {
                    break;
                }

            }
        }

        //rimuovi i giorni dell'primo mese
        $days -= $this->fromDate->day;
        //rimuovi i giorni dell'ultimo mese
        $days -= ($this->monthDay[$this->toDate->month - 1] - $this->toDate->day);

        $this->totalDays = $days;
    }
0 likes
16 replies
martinbean's avatar

need to calculate difference between two given dates without using PHP date functions or objects

@ilmala Why? If you really need to do so, then just compare two UNIX timestamps:

$start = strtotime('1 October 2016');
$end = strtotime('31 October 2016');

return $end - $start; // Difference in seconds. Do what you want with it here.
martinbean's avatar

@ilmala It’s not really testing your abilities if you get a forum to answer the question for you. You’re being tested, not us.

1 like
ilmala's avatar
Level 17

i don't ask the solution, but a tip or a different point o view .... !

willvincent's avatar

Ok, here's my point of view: This is a stupid test. Use proper tools to do the job you're trying to do. Your instructor is the picture in the book next to the axiom "those who can't do, teach"

As for a tip -- it doesn't matter if your code doesn't work as to my eye you didn't accomplish the idiotic requirement of not using any php function or objects.

ilmala's avatar
Level 17

It's a test send me to get a job interview. My only question was if there is a different way to do this? because my code don't work well compared with php function. Is a stupid test ... yes!

jlrdw's avatar

without using PHP date functions or objects

Just use Microsoft Excel or some other spreadsheet, probably the answer to test, didn't say another program.

Like the one famous college exam, question was why? One student answered why not and passed.

I'm sure --immck will have a brillant answer now.

jimmck's avatar

@jlrdw I don't do other peoples homework. And the OP should use your answer! It is as always on point. And exactly @willvincent and if this is an interview question would you want to work there? Poor question asked by someone who should not be interviewing.

2 likes
jlrdw's avatar

@jimmck I see you helping someone with sql server, My answer would be for sql server use razor / asp.net. Right on point.

ilmala's avatar
Level 17

I never ask to write code for me o do my homework!! I have posted my code and ask "Any idea/tips?". So i don't understand this kind of answers kids!

jlrdw's avatar

No @jimmck use the opportunity as your answers are always funny and make me smile and laugh.

Ooh ooh, I came up with an even better answer, Google it, that is the date difference. @jimmck I know will just fall in love with that answer.

willvincent's avatar
Level 54

@ilmala use martinbean's suggestion.. strtotime, get absolute value of date1 - date2, then parse out number of years, months, days, etc. from that resulting number of seconds.

Most people though, would probably just use carbon. or something like this

Seriously though.. this is a bullshit question, whoever asked it -- as has been said here -- shouldn't be interviewing people.

2 likes
jlrdw's avatar

No one even bothered to wish you luck getting the job so good luck.

1 like

Please or to participate in this conversation.