javascript - show saved utc time in database as invisitor's local lime based on his local time zone

I have a php script that adds a time along with other data in database :

<?php 

$UTC_TIME = gmdate("Y-m-d H:i:s"); //Gives : 2022-01-26 14:11:43

?>

Now, when the webpage is viewed at different locations in different countries. So i want to display the UTC time saved in database in local time of visitor who is accessing the webpage.

For example, if a user access the page from UK, the date time (2022-01-26 14:11:43) should be into converted into UK time (That was at the time, when this data was saved into the database) and displayed.

I searched alot of topics on stackoverflow, but didn't find any working solution. Thanks for your help!

Answer

Solution:

Just add a T and a Z

// $UTC_TIME = gmdate("Y-m-d\TH:i:s\Z"); //Gives : 2022-01-26T14:11:43Z
// const time = "<?=$UTC_TIME ?>";
const time = "2022-01-26T14:11:43Z"; // UTC
console.log(new Date(time).toLocaleString()); // my timezone

Source