Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading