$date = date('H'); if($date >= 22 && $date <= 04){ echo 'это ночь'; } else if($date >= 05 && $date <= 11){ echo 'это утро'; } else if($date >= 12 && $date <= 16){ echo 'это день'; } else if($date >= 17 && $date <=21){ echo 'это вечер'; } 

This code does not work in wordpress, date() changed to current_time() , did not help.

What's my mistake?

  • echo день'; => echo 'день' ; - E_p
  • I apologize, I made a typo, everything is correctly written in the source code - waspmax1
  • Tell me, but in this context, the file, or what is it, I do not understand, just without these, if echo does anything at all output? For example echo $ date; ? And in general there is an elseif condition without problems, it is more appropriate for the idea here - binliz
  • Tried to display the value of $ date? What does it mean is not working? - E_p
  • I just have an assumption that there is a buffered block through ob_start or something like that and nothing is output there - binliz

2 answers 2

Well, perhaps the problem is that the number can not be simultaneously more than 22 and less than 4:

if($date >= 22 && $date <= 04){

I recommend using date('G') which will return the hour without a zero.

Well, a small example of verification:

 function whatTheTime($date){ if($date >= 22 || $date <= 4){ return 'это ночь'; } else if($date >= 5 && $date <= 11){ return 'это утро'; } else if($date >= 12 && $date <= 16){ return 'это день'; } else if($date >= 17 && $date <=21){ return 'это вечер'; } } foreach(range(0, 24) as $hour){ echo $hour.' - '; echo whatTheTime($hour); echo "<br/>"; } 

Example

    The most common syntax error. echo день'; => echo 'день';

    To avoid them, install an editor that knows the syntax of PHP.

    From free:

    • Notepad ++
    • Atom
    • Microsoft Code
    • Brackets

    Some may need plugins.

    • A typo made in the question, in the source code echo is displayed in quotes - waspmax1