1
date_default_timezone_set("America/Chicago");
$time = time();
$hour = date("G",$time);
echo $hour;

    if(($hour >= 0) && ($hour <=3)) {
        $greeting == 'Good Evening';
    }
    elseif(($hour >= 4) && ($hour <=11)) {
        $greeting == 'Good Morning';
    }
    elseif(($hour >= 12) && ($hour <=18)){
        $greeting == 'Good Afternoon';
    }
    elseif(($hour >= 19) && ($hour <= 24)) {
        $greeting == 'Good Evening';
    }
echo $greeting;

First, I prefilled "America/Chicago", I would like this to be automatically detected based on current user's local time not UTC.

Second, as you can see, I echoed $hour, as a test, to see what was displaying. At the time I did this, $hour was 8. But, my if statements were never "true", therefore, no greeting was outputted, At the time, 8, should have displayed Good Morning as the greeting. I am positive I'm probably making some silly mistake, but I've been working with this code for three hours. Please help! Thank You!

Donald
  • 35
  • 1
  • 5
  • Looks like your using a comparison operator instead of an assignment operator for '$greeting' – slamborne Nov 21 '15 at 15:11
  • `$greeting ==` remove one of the equal signs for all of those and try again. If that fixed it, you can just delete the question. – Funk Forty Niner Nov 21 '15 at 15:14
  • How can you use an assignment operator instead? The comparison operator is doing just that, comparing. Comparing time, in order to say if this time equals (or is greater than) this time, then (condition) display this greeting? – Donald Nov 21 '15 at 15:14
  • the syntax is `$greeting =` to assign, not `$greeting ==` – Funk Forty Niner Nov 21 '15 at 15:14
  • I really need to learn how to take a break. I've been overworking my brain I didn't catch that. – Donald Nov 21 '15 at 15:15
  • believe me, it happens to everyone. – Funk Forty Niner Nov 21 '15 at 15:16
  • Is there an easy way to get the user's local timezone? I've found a lot of very complex methods. And I need it in a php variable, rather than javascript if possible. Thank You! – Donald Nov 21 '15 at 15:17
  • question first: by changing those to single equal signs; did it not solve your original question? – Funk Forty Niner Nov 21 '15 at 15:19
  • If you post it as an answer, I'll gladly give it a vote! – Donald Nov 21 '15 at 15:20
  • you're welcome. Well, if you want to get the user's current time, you will need to use a JS method, since servers work on their current timezone and not the users themselves. – Funk Forty Niner Nov 21 '15 at 15:20
  • No need to answer, there was already a duplicate question for it that I closed the question with. Thanks anyways though ;-) – Funk Forty Niner Nov 21 '15 at 15:21
  • ok, I don't mind using javascript if needed. I just want to set the default to their actual time. – Donald Nov 21 '15 at 15:23
  • Here, see this Q&A http://stackoverflow.com/questions/1091372/getting-the-clients-timezone-in-javascript - You can further your research using these keywords "get user's timezone javascript". – Funk Forty Niner Nov 21 '15 at 15:23
  • This is why I don't like working with javascript. Now, I have a number I can't do anything with. Say for example, I have `-300`, UTC+5 - how do I insert this into `date_default_timezone_set();`? – Donald Nov 21 '15 at 15:50
  • I appreciate the reference to that answer, but... it just gives you a useless value, doesn't actually set the timezone. – Donald Nov 21 '15 at 15:51
  • In your opinion, is it more reliable to have the user choose his/her own timezone? That's something I can easily make happen. – Donald Nov 21 '15 at 16:11
  • See [this answer](http://stackoverflow.com/a/22625076/634824) for getting the time zone. – Matt Johnson-Pint Nov 21 '15 at 23:39

0 Answers0