2

i uploaded a report website to an online server, then i tested it and found out that the time function i created to convert timestamp data in my db gives a wrong output.

And note that this only happened when it got uploaded online, the timestamp stores the right data into my db, but the time function i wrote in php is giving me wrong output.

For example, i made a report which stored the time of the report in my db with timestamp data type, but the time function gives an output of "5 hours ago", instead of "just now"....check my code below

The time function

date_default_timezone_set("Africa/Lagos");
function convertTime($timestamp)
{
    $elapsed_time = strtotime($timestamp);
    $current_time = time();
    $time_diff = $current_time - $elapsed_time;

    $seconds = $time_diff;
    $minutes = round($seconds / 60);
    $hours = round($seconds / 3600);
    $days = round($seconds / 86400);
    $weeks = round($seconds / 604800);
    $months = round($seconds / 2629440);
    $years = round($seconds / 31553280);

    if ($seconds <= 60) : return "Just Now"; endif;

    if ($minutes <= 60) : if ($minutes == 1) : return "1 minute ago"; else : return "$minutes minutes ago"; endif; endif;

    if ($hours <= 24) : if ($hours == 1) : return "1 hour ago"; else : return "$hours hours ago"; endif; endif;

    if ($days <= 7) : if ($days == 1) : return "1 day ago"; else : return "$days days ago"; endif; endif;

    if ($weeks <= 4.3) : if ($minutes == 1) : return "1 week ago"; else : return "$weeks weeks ago"; endif; endif;

    if ($months <= 12) : if ($months == 1) : return "1 month ago"; else : return "$months months ago"; endif; endif;

    if ($years == 1) : return "1 year ago"; else : return "$years years ago"; endif;
};

Updated

This is the output i get immediately i make a report

For example New Report Made 5 Hours Ago

What i expect to see is New Report Made Just Now

And then a minute later i expect to see New Report Made One Minute Ago

This was working fine in my localhost, until i uploaded it online

sancoLgates
  • 188
  • 12
Redemption Okoro
  • 349
  • 1
  • 11

1 Answers1

0

When dealing with timezones, there are 3 things you need to make the same in your localhost and your production server

  1. The date.timezone option in php.ini file

Which you already set on both machines by using date_default_timezone_set()

  1. Your code, including any dynamic timezone you set to date functions like $dt = new \DateTime("now", new \DateTimeZone('UTC'))

Which is also -according to what you say- the same between your both machines (your function is the same and you don't use dynamic timezones in your function )

  1. MySQL timezone the global or the session one (check here for details)

This options affects the timestamp data when you insert and select them

If you are using the same exactly 1- code, 2- PHP time zone and 3- MySQL timezone (when you inserted the data into your production server, and while you are selecting it in this PHP script) between the localhost and the live server, you are guaranteed to get the same results.

returning to your example

Everything between your localhost and live server is the same, except for 1 thing that is not covered by your post, the database timezone. Now do this test

function convertTime($timestamp)
{
    var_dump($timestamp);
    exit;
}

And run it on your localhost and your production server, I'm pretty sure you will get different values -if both machines have the same exact code you posted in the question-

If they are different, then there are 2 cases

  • you could have used another timezone when you populate your database when you uploaded your website to the server (that means that all your timestamps data stored is wrong, and needs to be updated)

  • or the data stored are the same but you are just using different MySQL timezones at the run time, make this in your code

    date_default_timezone_set("Africa/Lagos");
    $connection->query("SET time_zone = '+1:00'"); //your MySQLI object
    

If this didn't work, or in your MySQLWorkbench or PHPMyAdmin running this

SET time_zone = '+1:00';
select timezone from t1 order by timezone limit 1;

on both machines (having same data in MySQL) give different results, that means your data is changed during the move to the production server and it needs to be updated.

Accountant م
  • 6,975
  • 3
  • 41
  • 61