The same function with the same data gives me different results

I have a little code just to know when a message was sent:

function time_ago ($respuestas) {

$date = $respuestas;

$today_date = getdate();
$months = ['enero', 'febrero', 'marzo', 'abril', 'mayo', 'junio','julio','agosto','septiembre','octubre','noviembre','diciembre'];

$minute = $today_date['minutes'];
$hour = $today_date['hours'];
$day = $today_date['mday'];
$month = $today_date['mon'];

if ($today_date['hours'] <= 9) {
    $hour = "0" . $today_date['hours'];
}

if ($today_date['minutes'] <= 9) {
    $minute = "0" . $today_date['minutes'];
}

if ($today_date['mon'] <= 9) {
    $month = "0" . $today_date['mon'];
}

if ($today_date['mday'] <= 9) {
    $day = "0" . $today_date['mday'];
}

$actual_total_date = $today_date['year'] . "-" . $month . "-" . $day . " " . $hour . ":" . $minute;
$actual_total_date = new DateTime($actual_total_date);
//data of the date of the post
$post_date = substr($date,0,16);
$post_date = new DateTime($post_date);
$post_year = substr($date,0,4);
$post_month = substr($date,5,2);
$post_day = substr($date,8,2);
$post_hour = substr($date,11,2);
$post_minute = substr($date,14,2);
$interval = date_diff($post_date, $actual_total_date);
$invertal_days = substr($interval->format('%R%a'),1,strlen($interval->format('%R%a')));
$invertal_hours = $interval->format('%H');
$invertal_minutes = $interval->format('%I');

$result = "";

if ($invertal_days == 0 AND $invertal_hours == 0 AND $invertal_minutes < 1) {
    $result = "Now";
} else if ($invertal_days == 0 AND $invertal_hours == 0 AND $invertal_minutes == 1) {
    $result = substr($invertal_minutes,1,2) . " minute ago";
} else if ($invertal_days == 0 AND $invertal_hours == 0 AND $invertal_minutes < 10 AND $invertal_minutes > 1) {
    $result = substr($invertal_minutes,1,2) . " minutes ago";
} else if ($invertal_days == 0 AND $invertal_hours == 0 AND $invertal_minutes < 60 AND $invertal_minutes >= 10) {
    $result = $invertal_minutes . " minutes ago";
} else if ($invertal_days == 0 AND $post_day == $day) {
    $result = "Today at " . $post_hour . ":" . $post_minute ;
} else if ($invertal_days == 0 AND $post_day == $day-1) {
    $result = "Yesterday at " . $post_hour . ":" . $post_minute;
} else if ($invertal_days == 1 AND $post_day == $day-1) {
    $result = "Yesterday at " . $post_hour . ":" . $post_minute;
} else {
    $result = $post_day . " " . $months[$post_month-1] . " " . $post_year . " " . $post_hour . ":" . $post_minute;
}

return $result;

The problem I’m having only happens in production, not in local, and after doing some research I don’t know what is happening. What happens is that this function gives me two different outputs with the same input. For example with the date: 2017-07-09 20:52:39, at the time I’m sending this post, it should enter the fourth conditional, because the minutes interval is between 10 and 60 minutes.

I could share to the site where I’m having problems if needed. I have already tested the data I am using for the function and it is correct.

What is the value you are getting on the server? If I had to venture a guess, it is because the date/time of the server is in a different timezone. You can find that out using date_default_timezone_get and you can set a specific timezone using date_default_timezone_set (source)

I have check and is Europe/Berlin but both timezones are the same. So I don’t really know what’s happening. I have also set a timezone to Europe/Madrid, but it keeps telling me that is in Europe/Berlin

And what does it do? Can you give examples of the different returns it gives, that might make it easier to figure out what the difference is.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.