0

I'm looking for a solution where i can print (if possible by using a shortcode) the visitors current date, and not my/the server date. So if someone from New Zealand visits the site, it will say it is 15th of April, but if someone from London visits the site, it will display 14th of April.

Thanks Kind regards

  • I think this is a presentation layer issues, and so should be handled by the front end which is talking to your PHP backend site. Your PHP code can send out a GMT time, which the front end can then interpret in whatever the actual user timezone is. – Tim Biegeleisen Apr 14 '19 at 15:03
  • Hi Tim, first of all thanks for taking your time into answering my question. That being said, if all but a technical guy, although im keen to learn. So, although i think i understood your explanation, i have no clue on how to put it in practice, not even where to start. If you can help me that would be even more awesome. If not, Im sure you answer will help me and others to come up with a solution. Thanks – Andres Molina Perez-Tome Apr 14 '19 at 15:08

1 Answers1

1

If you simply want to display the local time from the client, you're better off using JavaScript than PHP (as PHP is executed on the server, JavaScript is executed client-side).
jQuery is an extension of JavaScript, and simplifies a lot of things like element-targeting and setting values, so for this example I'm using jQuery to set the text of the element.

The date object can be formatted in a number of ways, using toString() on it is just for demonstration purposes, you can format it however you like using the different getters (see the MDN).

 var date = new Date;
 
 $("#myTime").text(date.toString());
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<body>
  <div id="myTime"></div>
</body>
Qirel
  • 25,449
  • 7
  • 45
  • 62