Info on PHP's built in date() function. Bear in mind that date() will reflect the time on the server where your site is hosted, not necessarily the same as your visitors' local times. Getting the visitors' local time will require either asking them to enter their timezone, or obtaining it through javascript.

Also your if/else reasoning seemed overcomplicated, so I went with if, elseif, else instead.

Couple of additional thoughts to the OP, with all due respect to Darth.Titan who responded perfectly:

Please note that some users have no control over their current time zone, so none of these things are fool proof. But the JS method, being client side, is certainly the way to go unless you know all your users are from one centralized time zone location where you can adjust based on the offset in the PHP code.

If this really matters to you consider allowing users to select their time zone offset from GMT either in a preference pane or during login, saved to their account. Then you use the PHP method with that offset being calculated, no reliance on Javascript and very accurate time control as it's server based.

Browsing the forums, couldn't help notice a small bug in your code: when 10 AM, the script will display: "Good evening"

Replace with

else if (time>=10 && time<16)

The tip to use JavaScript due to TimeZones is a very good one.

I would also add that personally, I prefer to offload as much work as possible to the browsers (by JavaScript), in order to reduce server load (PHP). Consider how many people are using your website in the same time, when choosing to code certain processes in JavaScript or PHP.

Obviously, this is not the case with this script, but nevertheless, I consider it an important thing to keep in mind when building a website.

macrumors 68000

Thanks for the fix, it was not intended to be a copy/paste of code for the OP, just an untested example and I should have said so. As to your other comments, just a small note to add. As sites become more dynamic and complex and Unix timestamps are stored along with offsets into databases used for online calendars, events, forum postings like this, etc., server side processing is intensive anyway. Client side date stuff can also be generated and seeded by PHP in such situations and is an effective method. Otherwise I agree entirely with what you said about client side in general.

macrumors 65816

You can use php functions date_sunrise() and date_sunset() to get more accurate good mornings, good afternoons and good evenings if you wish.

I use javascript to get local time and use that to build the rest.

Critiques if I may( none of this is important, just 'traditional' styling ):
1) variables traditionally start with a small letter
2) all functions should have only one return
3) function names typically are camelCase or underscore_delimited

The latter approach can quickly become a mess of nested if-else statements as the number of checks increases, and especially if the actions to take on negative conditions are more diverse than returning an empty array.

macrumors 65816

I disagree with this advice. I find it vastly preferable to make quick exits from functions rather than add superfluous boolean or control flow statements.

Click to expand...

You and everyone else without significant experience in refactoring or maintaing code, but this has been a balanced argument for decades. Both with good points. So I agree with your disagreement.

Full disclosure: I use negation in conditionals and multiple returns every day. I only wrote what we should aspire to do ( single returns ). I'm currently refactoring code written just under a year ago by what I can only imagine was 3rd grade chipmonks. Why oh why! But I love my job. I make things pretty and easier to maintain.

PS. Both of your examples are common in first round draft code: negation in conditionals generally signifies poorly structured code ( probably due to lack of UML or prototyping ). Double return in second example. I could go on, but I have my broken code to fix.

macrumors 6502a

PS. Both of your examples are common in first round draft code: negation in conditionals generally signifies poorly structured code ( probably due to lack of UML or prototyping ). Double return in second example. I could go on, but I have my broken code to fix.

Click to expand...

Please do take the time to recode the two examples. I am genuinely interested in seeing how you would approach the problem in the most maintainable way.

MacRumors attracts a broad audience
of both consumers and professionals interested in
the latest technologies and products. We also boast an active community focused on
purchasing decisions and technical aspects of the iPhone, iPod, iPad, and Mac platforms.