2
I get the error
Notice: Object of class Dateinterval could not be converted to int
on line 78, where the second if
. The excerpt from the code is part of a while
, in that Timeframe
is calculated how long since the last posting of a user on a social network.
How can I fix this mistake?
//Timeframe
$date_time_row = date("Y-m-d H:i:s");
$start_date = new DateTime($date_time); //Time of post
$end_date = new DateTime($date_time_row); //Current time
$interval = $start_date->diff($end_date); //Difference between dates
if($interval->y >= 1) {
if($interval == 1)
$time_message = $interval->y . " year ago"; //1 year ago
else
$time_message = $interval->y . " years ago"; //1+ year ago
}
When you post something, it is reset when I refresh the page, showing an error for each user post. Posts are shown correctly at the bottom of the page, but the error remains.
I used
$interval = true;
if($interval == 1)
and I get the error: Trying to get Property 'y' of non-object, same for months, days and hours.– André Cavalcante
@Andrécavalcante yes... But it’s not for you to do this. This was just to show that php, does this conversion naturally. Use the first example of if I showed you.
– Andrei Coelho