0

So I am currently trying to send a greeting in a heading in my page. It should say "Good Day", or "Good Evening", etc., if the time is </> but = to the users time/timezone. But when receiving the greeting, it doesn't show the right one I've set for smaller than, or greater than, according to my timezone. I am in Germany. And people are from everywhere. So is there a way to receive the right echo according to your timezone? And if so, I would like the echos to display, when the timezone of the visitor is = to the hours set.

<?php
$time = date("H");
if (time < "4") {echo '<h1 id="Welcm">Welcome, Good Night</h1>';}
else if (time < "12") {echo '<h1 id="Welcm">Welcome, Good Morning</h1>';}
else if (time > "12") {echo '<h1 id="Welcm">Welcome, Good Day</h1>';}
else if (time > "18") {echo '<h1 id="Welcm">Welcome, Good Evening</h1>';}
else if (time > "23") {echo '<h1 id="Welcm">Welcome, Good Night</h1>';}
else {echo '<h1 id="Welcm">Welcome, Good Day</h1>';}

 ?>

Sorry, didn't know which snippet I should pick for PHP as there is none for PHP.

So this works, like it should, but not in the right timezone. I want the right greeting to appear according to the timezone which is always visitor specific. Like the right one should appear for Amerika/New_York, but also for Germany/Berlin and so on. If this doesn't, the greeting is worth- and workless.

Steve E.
  • 9,003
  • 6
  • 39
  • 57
xFgn
  • 37
  • 7

1 Answers1

1

Because PHP runs on server, it can't retrieve user's timezone. Luckily, Javascript runs on client. You can use Javascript to get user's timezone and send that to server using Ajax.

To get timezone with Javascript:

var d = new Date()
var n = d.getTimezoneOffset();

You can check here for more detail code: Determine a User's Timezone

Community
  • 1
  • 1
  • Or since it is a simple greeting it can be implemented completely in javascript. You don't have to do it server-side for any reason here – nikos.svnk May 04 '17 at 03:04
  • @nikos.svnk totally agree – Chandarong Nuon May 04 '17 at 03:05
  • Well when trying to implement, the time that java gets is either wrong, or the script doesn't load when trying to follow the given var's or w3schools.com – xFgn May 04 '17 at 04:28
  • I understood, but this didn't fix my issue. Yeah it is displaying how much hours I am off, but doesn't provide a solution to my problem currently. I've also tried around a little bit, but I couldn't find a solution so far myself. – xFgn May 05 '17 at 03:21