Home > Net >  show saved utc time in database as invisitor's local lime based on his local time zone
show saved utc time in database as invisitor's local lime based on his local time zone

Time:01-27

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!

CodePudding user response:

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

CodePudding user response:

$UTC_TIME = gmdate("Y-m-d\TH:i:s\Z"); //Gives : 2022-01-26T14:11:43Z

On the client side with js

new Date(date).toLocaleString(); //date is loaded from database
  •  Tags:  
  • Related