skip to Main Content

The code is not working as expected, please help. I want to display the time that I grab from the database as UTC into browser time.

`Latest data at:

latest_data_timestamp”;
$time = strtotime($utc);
echo ” “. date(“Y-m-d H:i:s”, $time).”
“;

?>

`

2

Answers


  1. For this you have to store the user’s timezone in your DB. If you are not storing the TimeZone the you have to perform with Javascript.

    <script>
    // Get UTC time from Blade template
    var utcTime = "{{ $utcTime->toIso8601String() }}";
    
    // Convert UTC time to local time
    var userLocalTime = new Date(utcTime);
    var userTimeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;
    var userTimezoneOffset = new Date().getTimezoneOffset();
    userLocalTime.setMinutes(userLocalTime.getMinutes() - userTimezoneOffset);
    
    // Display user's local time
    document.write("<div>User's Local Time: " + userLocalTime.toLocaleString() + "</div>");
    
    Login or Signup to reply.
  2. To convert UTC time to the user’s browser time in Laravel Blade, you can use JavaScript to handle the conversion client-side. Here’s a simple example of how you can achieve this:

    <!DOCTYPE html>
    <html>
    <head>
        <title>UTC to User Time</title>
    </head>
    <body>
        <p>UTC Time: {{ $utc_time }}</p>
        
        <script>
            var utcTime = new Date('{{ $utc_time }}');
            var userTime = new Date(utcTime.getTime() + (utcTime.getTimezoneOffset() * 60000));
            
            document.write('User Time: ' + userTime);
        </script>
    </body>
    </html>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search