Jump to content

[SOLVED] Time shows correctly, text not


MrRock

Recommended Posts

Hello,

 

This is my first post here, and I need your help.

A friend of mine asked me to find him something that will show about which playlist is playing on his site. He is changing it according to the time. The script reports the time correctly. But, even if it is 16:00, it will still show "Alternative Rock/Metal" on the text result. What's wrong with the coding?

 

<?php
$h = date('G:i'); //set variable $h to the hour of the day
//G is the date key for hours in 24 format (not 12), with no leading 0s, like 02.

if ($h >= '0:00') $txt = 'Alternative Rock/Metal';
else if ($h >= '6:00') $txt = 'Hard Rock';
else if ($h >= '10:00') $txt = 'Progressive Rock/Metal';
else if ($h >= '11:00') $txt = 'Various Songs';
else if ($h >= '15:00') $txt = 'Gothic/.../Power Metal';
else if ($h >= '17:00') $txt = 'Heavy Metal';
else if ($h >= '20:00') $txt = 'Other Genres';
else if ($h >= '22:00') $txt = 'Ballads';
else $txt = '?';

echo 'The time is ' . $h . ' and you listen to ' . $txt . ' playlist.';
?>

 

It seems right to me, but it isn't...

Link to comment
https://forums.phpfreaks.com/topic/123373-solved-time-shows-correctly-text-not/
Share on other sites

I think i did it. Will this work right?

$h = date('G:i'); //set variable $h to the hour of the day
//G is the date key for hours in 24 format (not 12), with no leading 0s, like 02.
if ($h <= '23:59') $txt = 'Ballads';
else if ($h <= '21:59') $txt = 'Other Genres';
else if ($h <= '19:59') $txt = 'Heavy Metal';
else if ($h <= '16:59') $txt = 'Gothic/.../Power Metal';
else if ($h <= '14:59') $txt = 'Various Songs';
else if ($h <= '10:59') $txt = 'Progressive Rock/Metal';
else if ($h <= '9:59') $txt = 'Hard Rock';
else if ($h <= '5:59') $txt = 'Alternative Rock/Metal';
else $txt = 'Stupid programmer alert';

this will do what you want:

 

<?php
$h = date('G:i'); //set variable $h to the hour of the day
//G is the date key for hours in 24 format (not 12), with no leading 0s, like 02.

$txt = 'Alternative Rock/Metal';

$test_var = (int) $h;
if ($test_var < 22) $txt = 'Ballads';
if ($test_var < 20) $txt = 'Other Genres';
if ($test_var < 17) $txt = 'Heavy Metal';
if ($test_var < 15) $txt = 'Gothic/.../Power Metal';
if ($test_var < 11) $txt = 'Various Songs';
if ($test_var < 10) $txt = 'Progressive Rock/Metal';
if ($test_var < 6) $txt = 'Hard Rock';
if ($test_var < 5 || $test_var > 22) $txt = 'SOME OTHER MUSIC';

echo 'The time is ' . $h . ' and you listen to ' . $txt . ' playlist.';

?>

 

tested and working on my machine

Thank you for your reply, and for the code.

 

I've done some modifications to make the times & playlist as they were supposed to, so the code is like this:

 

<?php
$h = date('G:i'); //set variable $h to the hour of the day
//G is the date key for hours in 24 format (not 12), with no leading 0s, like 02.

$txt = 'Alternative Rock/Metal';

$test_var = (int) $h;
if ($test_var <= 23) $txt = 'Ballads';
if ($test_var < 22) $txt = 'Other Genres';
if ($test_var < 20) $txt = 'Heavy Metal';
if ($test_var < 17) $txt = 'Gothic/.../Power Metal';
if ($test_var < 15) $txt = 'Various Songs';
if ($test_var < 11) $txt = 'Progressive Rock/Metal';
if ($test_var < 10) $txt = 'Hard Rock';
if ($test_var < 6 || $test_var > 23) $txt = 'Alternative Rock/Metal';
echo ' and you are listening to ' . $txt . ' playlist.';
?>

The "$test_var > 23" can be removed safely, right?

It will still work, right?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.