Morning, afternoon, evening php help

Chris S

Retired
Messages
2,055
Reaction score
1
Points
38
I have the following code and it is supposed to tell you the "time of day" basically morning, afternoon, or evening but it keeps on poping out morning


PHP:
$date = date("G");
echo $date;

if (($date) == '01' OR '02' OR '03' OR '04' OR '05' OR '06' OR '07' OR '08' OR '09' OR '10' OR '11' OR '12'){
$time = 'morning';
}elseif('13' OR '14' OR '15' OR '16' OR '17' OR '18'){
$time = 'afternoon';
}elseif('19' OR '20' OR '21' OR '22' OR '23' OR '24'){
$time = 'evening';
}else{
$time = 'sorry';
}
 

Conor

New Member
Messages
3,570
Reaction score
0
Points
0
Not a PHP guru but are you sure its a 24 hour clock?
 

Chris S

Retired
Messages
2,055
Reaction score
1
Points
38
ya i made that mistake earlier. G is 24 with leading zero
 

Bryon

I Fix Things
Messages
8,149
Reaction score
101
Points
48
Why not like..

PHP:
if ($date <= 09) {
   $time = 'morning';
}
elseif ($date >= 10) AND ($date <= 18) {
   $time = 'afternoon';
}
elseif($date >= 19) {
   $time = 'evening';
}
else{
   $time = 'sorry';
}

I havent' tested it.. But you should get the idea.
 

Chris S

Retired
Messages
2,055
Reaction score
1
Points
38
Bryon said:
Why not like..

PHP:
if ($date <= 09) {
   $time = 'morning';
}
elseif ($date >= 10) AND ($date <= 18) {
   $time = 'afternoon';
}
elseif($date >= 19) {
   $time = 'evening';
}
else{
   $time = 'sorry';
}

I havent' tested it.. But you should get the idea.

because im a noob at some types of php

thanks, this is what worked

PHP:
if ($date <= 09) {
   $time = 'morning';
}
elseif ($date >= 10 AND $date <= 18) {
   $time = 'afternoon';
}
elseif($date >= 19) {
   $time = 'evening';
}
else{
   $time = 'sorry';
}

there was 1 small error
 
Last edited:

Bryon

I Fix Things
Messages
8,149
Reaction score
101
Points
48
elseif (($date >= 10) AND ($date <= 18)) {

;)
 
M

minievan

Guest
Im paying someone to teach me php, and so far I only know variables and the basics XD

Good luck if you havnt sorted it out anyway..
 
Top