Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Special Interests > Visual Media > Web Design and Development

Reply
 
Thread Tools Search this Thread Display Modes
Old Oct 22, 2012, 07:25 PM   #1
jacob.3336
macrumors newbie
 
Join Date: Jul 2012
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:
function timeofday(){

if (
$Time <= "5:00 PM") {
return 
"Evening";
}else{

if (
$Time <= "12:00 PM") {
return 
"Afternoon";
}else{

if (
$Time <= "5:00 AM") {
return 
"Morning";
}
}

It doesn't work.
Does anybody know how I could make it work.
jacob.3336 is offline   1 Reply With Quote
Old Oct 22, 2012, 08:54 PM   #2
Darth.Titan
macrumors 68020
 
Darth.Titan's Avatar
 
Join Date: Oct 2007
  • You're not setting a value for $time.
  • You're using comparison operators on string values. i.e. "5:00 PM" is a string value.

Think 24-hour clock time:
PHP Code:
function timeofday(){ 
    
$time date('G');
    
    if (
$time 12) { 
        return 
"Morning"
    }
    elseif (
$time >= 12 && $time 17) { 
        return 
"Afternoon"
    }
    else{ 
        return 
"Evening"
    } 

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.
Darth.Titan is offline   0 Reply With Quote
Old Oct 23, 2012, 12:44 PM   #3
SrWebDeveloper
macrumors 68000
 
SrWebDeveloper's Avatar
 
Join Date: Dec 2007
Location: Alexandria, VA, USA
 
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>
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.
__________________
Jim Goldbloom
Sr. Web Developer, owner GoldTechPro, LLC
http://www.GoldTechPro.com
SrWebDeveloper is offline   1 Reply With Quote
Old Nov 8, 2012, 01:17 PM   #4
Moshu
macrumors member
 
Join Date: May 2012
Quote:
Originally Posted by SrWebDeveloper View Post
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>
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.

Cheers!
__________________
iPhone 5s 16GB iPad Mini Retina 3G 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.
Moshu is offline   0 Reply With Quote
Old Nov 9, 2012, 03:40 AM   #5
SrWebDeveloper
macrumors 68000
 
SrWebDeveloper's Avatar
 
Join Date: Dec 2007
Location: Alexandria, VA, USA
 
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
SrWebDeveloper is offline   0 Reply With Quote
Old Jun 8, 2013, 03:08 PM   #6
imzac
macrumors newbie
 
Join Date: Jun 2013
Quote:
Originally Posted by Darth.Titan View Post
  • You're not setting a value for $time.
  • You're using comparison operators on string values. i.e. "5:00 PM" is a string value.

Think 24-hour clock time:
PHP Code:
function timeofday(){ 
    
$time date('G');
    
    if (
$time 12) { 
        return 
"Morning"
    }
    elseif (
$time >= 12 && $time 17) { 
        return 
"Afternoon"
    }
    else{ 
        return 
"Evening"
    } 

Is it possible to make this work with minutes as well?

PHP Code:
$hour date('G'); 
$min date('i'); 

if (
$hour 9) {  
        echo 
"Shouldn't you be asleep? Yeah, we thought so. ";  
    } 
  elseif (
$hour == && $min 30) {  
        echo 
"Rise and shine! Get out of bed!";  
    }
  elseif (
$hour == && $min 30) {  
        echo 
"You should be headed to breakfast now.";  
    } 
Is that wrong?
imzac is offline   0 Reply With Quote
Old Jun 13, 2013, 04:22 AM   #7
johnmacd
macrumors newbie
 
Join Date: Jun 2013
To disply time of the day function you need use time() function.
And compaire that time with your variable if it matches then show the msg.
johnmacd is offline   0 Reply With Quote
Old Aug 30, 2013, 02:07 AM   #8
geekrew
macrumors newbie
 
Join Date: Aug 2013
function timeofday(){
$time = date('G');

if ($time < 12) {
return "Morning";
}
elseif ($time >= 12 && $time < 17) {
return "Afternoon";
}
else{
return "Evening";
}
}

you have not used the function date that's why it was not working.
geekrew is offline   0 Reply With Quote
Old Aug 30, 2013, 07:00 AM   #9
960design
macrumors 6502a
 
Join Date: Apr 2012
Location: Destin, FL
Just for obnoxious completeness:

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
__________________
TI-99/4A, tape cassette, 12" B&W Zenith
960design is offline   0 Reply With Quote
Old Oct 7, 2013, 04:21 AM   #10
trenthanover
macrumors newbie
 
Join Date: Oct 2013
function timeofday(){
$time = date('G');

if ($time < 12) {
return "Morning";
}
elseif ($time >= 12 && $time < 17) {
return "Afternoon";
}
else{
return "Evening";
}
}
trenthanover is offline   0 Reply With Quote
Old Oct 7, 2013, 07:41 AM   #11
ohbrilliance
macrumors 6502a
 
Join Date: May 2007
Location: Melbourne, Australia
Quote:
Originally Posted by 960design View Post
2) all functions should have only one return
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.

As a simple example, compare these two for clarity. With multiple return statements:

Code:
function getTransactionsForUser($userId='')
{
   if (!$userId) {
      return false;
   }

   if (!($user = getUserForId($userId)) {
      return false;
   }

   // Here we do lots of stuff to gather transactions for the given user

   return $transactions;
}
With a single return statement:

Code:
function getTransactionsForUser($userId='')
{
   $transactions = false;

   if ($userId) {
      if ($user = getUserForId($userId) {
         // Here we do lots of stuff to gather transactions for the given user
         return $transactions;
      }
   }

   return $transactions;
}
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.
ohbrilliance is offline   0 Reply With Quote
Old Oct 9, 2013, 07:22 AM   #12
960design
macrumors 6502a
 
Join Date: Apr 2012
Location: Destin, FL
Quote:
Originally Posted by ohbrilliance View Post
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.
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.
__________________
TI-99/4A, tape cassette, 12" B&W Zenith
960design is offline   0 Reply With Quote
Old Oct 9, 2013, 08:53 AM   #13
ohbrilliance
macrumors 6502a
 
Join Date: May 2007
Location: Melbourne, Australia
Quote:
Originally Posted by 960design View Post
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.
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.
ohbrilliance is offline   0 Reply With Quote
Old Oct 18, 2013, 08:32 AM   #14
markmiller988
macrumors newbie
 
Join Date: Apr 2013
Use Function

function timeofday(){
$time = date('G');

if ($time < 12) {
return "Morning";
}
elseif ($time >= 12 && $time < 17) {
return "Afternoon";
}
else{
return "Evening";
}
}

Try this and get result.
markmiller988 is offline   0 Reply With Quote
Old Oct 19, 2013, 02:23 AM   #15
SrWebDeveloper
macrumors 68000
 
SrWebDeveloper's Avatar
 
Join Date: Dec 2007
Location: Alexandria, VA, USA
 
Quote:
Originally Posted by markmiller988 View Post
function timeofday(){
$time = date('G');

if ($time < 12) {
return "Morning";
}
elseif ($time >= 12 && $time < 17) {
return "Afternoon";
}
else{
return "Evening";
}
}

Try this and get result.
This is *exactly* the same solution posed by another user in reply #10 previous to this one.
__________________
Jim Goldbloom
Sr. Web Developer, owner GoldTechPro, LLC
http://www.GoldTechPro.com
SrWebDeveloper is offline   0 Reply With Quote

Reply
MacRumors Forums > Special Interests > Visual Media > Web Design and Development

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
[php] - php problems os x mountain lion 10.8.2 cakas6933 OS X 10.8 Mountain Lion 4 Jun 10, 2014 08:08 AM
Calendar bug in 7.1 B3 when selecting day and time. richard371 iOS 7 2 Jan 17, 2014 07:17 PM
What time in the day did your 5S/5C deliver? ethereal45 iPhone 16 Oct 17, 2013 09:17 AM
Time Machine Backup Once Per Day, Possible? 2012Tony2012 iMac 7 Dec 3, 2012 05:39 AM
Restoring to previous day w/o Time Machine EvanMcKnight OS X 1 Sep 23, 2012 10:34 AM

Forum Jump

All times are GMT -5. The time now is 03:01 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC