Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
0

Time using switch function

Guest
Apr 15, 2007 Apr 15, 2007

Copy link to clipboard

Copied

I am trying to get my php script to say Good morning and then Good afternoon using military time. I can't see to get the script to run properly. Here is what I have, what am I missing.

<?php
$today = date("G");

switch(date("G")){
case(date("G") <= 12):
echo "Good Morining";
break;
case(date("G") >= 13):
echo "Good Afternoon";
default:
echo "Cannot Determine";
}

echo $today;
?>

Thank you for your help.
TOPICS
Server side applications

Views

495
Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 15, 2007 Apr 15, 2007

Copy link to clipboard

Copied

On Sun, 15 Apr 2007 13:35:55 +0000 (UTC), "AdonaiEchad"
<webforumsuser@macromedia.com> wrote:

>I am trying to get my php script to say Good morning and then Good afternoon
>using military time. I can't see to get the script to run properly. Here is
>what I have, what am I missing.

You're missing a break after "Good Afternoon".
--
Steve Fleischer
steve at flyingtigerwebdesign dot com
Hong Kong

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 15, 2007 Apr 15, 2007

Copy link to clipboard

Copied

On Sun, 15 Apr 2007 13:35:55 +0000 (UTC), "AdonaiEchad"
<webforumsuser@macromedia.com> wrote:

>I am trying to get my php script to say Good morning and then Good afternoon
>using military time. I can't see to get the script to run properly. Here is
>what I have, what am I missing.

You seem to be overcomplicating it a bit. Why not just:

<?php
print 'Good '.(date('G')<12?'morning':'afternoon');
?>

Gary

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 15, 2007 Apr 15, 2007

Copy link to clipboard

Copied

AdonaiEchad wrote:
> I am trying to get my php script to say Good morning and then Good afternoon
> using military time. I can't see to get the script to run properly. Here is
> what I have, what am I missing.

There are many mistakes in that script. First of all, if you're
assigning date("G") to a variable ($today), you should be using the
variable instead of constantly repeating it. Secondly, you cannot use
comparisons in a case expression. Only integers, floating-point numbers,
or strings are allowed. Thirdly, even if your switch statement worked
(which it won't), you have omitted break after the second case expression.

http://www.php.net/manual/en/control-structures.switch.php

The solution that Gary has given you is the shortest, but may not be
quite so easy to read if you're not familiar with the conditional operator.

This might be easier to read:

<?php
$hour = date('G');
if ($hour < 12) {
$greeting = 'morning';
}
elseif ($hour < 18) {
$greeting = 'afternoon';
}
else {
$greeting = 'evening';
}
echo "Good $greeting";
?>

--
David Powers, Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guest
Apr 15, 2007 Apr 15, 2007

Copy link to clipboard

Copied

You are right. First I should of seen the break in the switch statement. I should of used the if statement, I thought the switch would of been easier. Thank you both for your quick response.

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 15, 2007 Apr 15, 2007

Copy link to clipboard

Copied

On Sun, 15 Apr 2007 20:54:54 +0000 (UTC), "AdonaiEchad"
<webforumsuser@macromedia.com> wrote:

>You are right. First I should of seen the break in the switch statement. I should of used the if statement, I thought the switch would of been easier. Thank you both for your quick response.

For the record, the OP's original code does work (after adding the
missing break). I tested this in phpED:

<?php
date_default_timezone_set("Asia/Hong_Kong");
$today = (date("G"));

switch(date("G")){
case(date("G") <= 12):
echo "Good Morining";
break;
case(date("G") >= 13):
echo "Good Afternoon";
break;
default:
echo "Cannot Determine";
}
?>
--
Steve Fleischer
steve at flyingtigerwebdesign dot com
Hong Kong

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 16, 2007 Apr 16, 2007

Copy link to clipboard

Copied

Steve Fleischer wrote:
> For the record, the OP's original code does work (after adding the
> missing break). I tested this in phpED:
>
> <?php
> date_default_timezone_set("Asia/Hong_Kong");
> $today = (date("G"));
>
> switch(date("G")){
> case(date("G") <= 12):
> echo "Good Morining";
> break;
> case(date("G") >= 13):
> echo "Good Afternoon";
> break;
> default:
> echo "Cannot Determine";
> }
> ?>

I stand corrected. Presumably, the parser is evaluating the comparison
first, and producing a scalar value of true or false for the case
expression.

Nevertheless, it's a complete misuse of the switch control structure,
and will produce the wrong result between midnight and 1 a.m. What will
happen is that date("G") will produce 0. The first case expression will
produce true and the second one false. Since 0 equates to false, the
result will be "Good Afternoon", which is incorrect.

--
David Powers, Adobe Community Expert
Author, "Foundation PHP for Dreamweaver 8" (friends of ED)
Author, "PHP Solutions" (friends of ED)
http://foundationphp.com/

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 16, 2007 Apr 16, 2007

Copy link to clipboard

Copied

LATEST
On Mon, 16 Apr 2007 11:04:44 +0100, David Powers <david@example.com>
wrote:

>Nevertheless, it's a complete misuse of the switch control structure

Agreed.
--
Steve Fleischer
steve at flyingtigerwebdesign dot com
Hong Kong

Votes

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines