Jump to content

Simple time function


zenix

Recommended Posts

Hi, I am learning php and have been doing alright figuring things out myself...until this. I need a scrpt that will determine what time it is and post a message accordingly. For instance, if it's morning I'd like the page to read, Good Morning. The same for afternoon and evening. I have attempted multiple if/else statements and case switch statements. I MUST be over looking somehting. Here is the case statement:

 

$time = date("G i" ,time());

 

echo "<br>";

echo "$time";

 

switch ($time)

{

  case 0:

  if ($time > 1200)

  echo "<br>";

  echo "Good Afternoon!<br>";

  break;

  case 1:

  if ($time > 1200 || $time < 1659)

  echo "<br>";

  echo "Good Afternoon!<br>";

  break;

  case 1:

  if ($time > 1700)

  echo "<br>";

  echo "Good Evening!<br>";

  break;

 

}

 

To me it looks as thoughit should work....but what do I know? Apparently not much about this. Any guidence would be very much appreciated.

Link to comment
Share on other sites

Remove the switch, you don't need that. Switches take the argument variable and if it is equal to 0 it'll run case 0, if it's equal to 1 it'll run case 1 and so on...

 

Try something like this:

 

$time = date("G i" ,time());

echo "<br>";
echo "$time";

  if ($time > 1200){
  echo "<br>";
  echo "Good Afternoon!<br>";
  }elseif ($time > 1200 || $time < 1659){
  echo "<br>";
  echo "Good Afternoon!<br>";
  }elseif ($time > 1700){
  echo "<br>";
  echo "Good Evening!<br>";
  }

Link to comment
Share on other sites

Remove the switch, you don't need that. Switches take the argument variable and if it is equal to 0 it'll run case 0, if it's equal to 1 it'll run case 1 and so on...

 

Try something like this:

 

$time = date("G i" ,time());

echo "<br>";
echo "$time";

  if ($time > 1200){
  echo "<br>";
  echo "Good Afternoon!<br>";
  }elseif ($time > 1200 || $time < 1659){
  echo "<br>";
  echo "Good Afternoon!<br>";
  }elseif ($time > 1700){
  echo "<br>";
  echo "Good Evening!<br>";
  }

 

Thanks an awful lot for your input! Just to experiment, I copied and pasted your code into a new page, so I could see how it would work. My next step of course is to figure out how each part works so I learn better. Anyway, no matter what happens it still says good Afternoon. I have changed the time on the computer and the page reflects this change, but it still only says good morning. I also tried changing the if($time > 1200) to read if($time < 1200)...and changed the first echo to Good Morning. Now it only displays good morning. Thanks for the intro to elseif, basic to you I'm sure, but looks like a handy tool!

Link to comment
Share on other sites

Okay I looked at your if() statements and they're a bit muddled.

 

It will always say Good Morning if the time is greater then 12. This is because it stops at the first if(), you need to add something else in there.

 

Take a look at this (I've commented it):

<?php
$time = (int) date("Gi" ,time());

echo "<br>";
echo "$time";

  if($time > 0 && $time < 1159){ //Check if the current time is greater then 0 and also if it is less than 1159
  echo "<br>";
  echo "Good Morning!<br>";
  }elseif($time > 1200 && $time < 1659){ //If it wasn't the above, now check if it is greater than 12 but also check to see if it is less than 1659
  echo "<br>";
  echo "Good Afternoon!<br>";
  }elseif($time > 1700 && $time < 2400){ //This can just be }else{ but it doesn't matter. If the time is greater than 1700 and less than 2400 it must be evening
  echo "<br>";
  echo "Good Evening!<br>";
  }
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.