2

I've created a function to return the difference between two dates

<?php


class days {
    function dateDiff($start, $end) {
        $start_ts = strtotime($start);

        $end_ts = strtotime($end);

        $diff = $end_ts - $start_ts;

        $diff1 = ceil($diff / 86400);

        return $diff1;

    }
}

I have this code in the view :

<?php
$a    = new days();
$days = $a->dateDiff($v[17], date('Y/m/d'));
if ($days < 30) {
    $ds = $days;
    $tm = 'days';
} else {
    if ($days < 365) {
        $ds = $days / 30;
        $tm = 'months';
    } else {
        $ds = $days / 365;
        $tm = 'years';
    }
}

$v[17] is the date returned from the database to the view.

When I enter for instance an article in august 2011... It will display :

2.9666666666667 months ago

I ask myself ... How this Ceil method could not return an int value as it's supposed to do?

if that's normal, then what's the solution?

Thank you in advance :)

Martin.
  • 10,494
  • 3
  • 42
  • 68
SmootQ
  • 2,096
  • 7
  • 33
  • 58
  • 1
    ceil is returning an int and then you turn it to float again here `$ds=$days/30` – Esailija Nov 13 '11 at 14:16
  • 2
    You should also remember that if you do any variable type checking, that ceil(), floor(), and round() do actually return floats, they're just rounded to the nearest integer ;) http://uk.php.net/manual/en/function.ceil.php – Chris Nov 13 '11 at 14:24
  • Thank you all, I now know where's the problem , thank you Esailija, thank you Chris :-) +1 – SmootQ Nov 13 '11 at 14:28
  • 1
    Your logic is flawed. You are assuming that each month has 30 days, each year has 365 days. You might want to take a look at [this post](http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php/676828#676828). If you are running php > 5.3, you might want to checkout [DateInterval object](http://www.php.net/manual/en/datetime.diff.php) – circusbred Nov 13 '11 at 14:40
  • Of course dateInterval , you're right, But this is just an initial code, I should've do some other updates in the code... one of the updates is to integrate the ifs in the functions and return an html code for the small displayer of the time. thank you – SmootQ Nov 13 '11 at 14:46

1 Answers1

3

The ceil funciton works just fine when it returns the number of days.

But the problem is here:

if ($days<365){
   $ds=$days/30;
   $tm='months';
}

You didn't use ceil this time! You should try something like $ds = ceil($days / 30);.

Same thing for the number of years.

It would probably be more precise to use round instead of ceil, so that 32 days don't translate in 2 months:

$days = $a->dateDiff('10 oct 2011',date('Y/m/d'));

if ($days < 30) {
    $ds = $days;
    $tm = 'day';
}
else {
    if ($days < 365){
        $ds = round($days / 30);
        $tm = 'month';
    }
    else {
        $ds = round($days / 365);
        $tm = 'year';
    }
}

if ($ds > 1) {
    $tm .= 's';
}

echo "$ds $tm"; # => 1 month; or 2 months using ceil function
alexpirine
  • 3,023
  • 1
  • 26
  • 41