|
|
#1 |
|
PHP Time Of Day
I am making a web application.
One thing is needs to do is get the time of day so it can say for example in the evening: "Good Evening, User" I have tried an if statement: PHP Code:
Does anybody know how I could make it work. |
|
|
|
1
|
|
|
#2 |
Think 24-hour clock time: PHP Code:
Also your if/else reasoning seemed overcomplicated, so I went with if, elseif, else instead. |
|
|
|
0
|
|
|
#3 |
|
For client side Javascript (local browser time):
Code:
<script type="text/javascript">
var d = new Date()
var time = d.getHours()
if (time<10)
{
document.write("<b>Good morning</b>");
}
else if (time>10 && time<16)
{
document.write("<b>Good afternoon</b>");
}
else
{
document.write("<b>Good evening</b>");
}
</script>
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.
__________________
Jim Goldbloom Sr. Web Developer, owner GoldTechPro, LLC http://www.GoldTechPro.com
|
|
|
|
0
|
|
|
#4 | |
|
Quote:
![]() 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. Cheers!
__________________
iPhone 4s 16GB iPad mini WiFi 16GB 2012 13" MacBook Air i5 4GB 256 GB 2011 Airport Extreme 2012 Airport Express
Last edited by Moshu; Nov 8, 2012 at 01:24 PM. |
||
|
|
0
|
|
|
#5 |
|
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.
__________________
Jim Goldbloom Sr. Web Developer, owner GoldTechPro, LLC http://www.GoldTechPro.com
|
|
|
|
0
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
All times are GMT -5. The time now is 04:58 AM.







Jim Goldbloom

Linear Mode
