0

I have been looking through various posts on here to find my solution but i can't find one :(

so what i am after is a way of finding the exact difference between to dates. I currently have the following code

$date = time();
$publishedDate = strtotime($item->published_time);
$datediff = $date - $publishedDate;
$daysListed = floor($datediff/(60*60*24));

if($daysListed >= 2)
{
    $days = $daysListed." days ago";    
}
elseif ($daysListed == 1)
{
    $days = $daysListed." yesterday";   
}
elseif ($daysListed < 1)
{
    $days = "today";
}

which does a pretty good job, the main flaw i have with this is say my user submits a post at 23:59pm last night the above code will return a value of "today" because $datediff is returning less than 24 hours, even though it was posted yesterday. Is there a more accurate way of getting the correct value returned?

Appreciate your help Luke

BBLJ84
  • 175
  • 1
  • 12
  • Use [DateTime objects](http://www.php.net/manual/en/class.datetime.php), and the `diff()` method, and then the answer is in the `days` property of the resulting DateInterval object – Mark Baker Dec 03 '15 at 13:11
  • thanks for your comments, Machavity my problem is slightly different to what was discussed on that thread. Mark Baker I'm quite the novice when it comes to this sort of stuff and i was unable to find what i was looking for in your links. – BBLJ84 Dec 03 '15 at 13:28

1 Answers1

1

You need to compare also the time
e.g. $timePub 23:59 $nowtime 11:20

....
elseif ($daysListed < 1)
{
  if ($timePub > $nowtime) {
    $days = " yesterday";
  }
  else {
    $days = "today";
  }  
}
moskito-x
  • 11,832
  • 5
  • 47
  • 60