MrRock Posted September 9, 2008 Share Posted September 9, 2008 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 More sharing options...
tbare Posted September 9, 2008 Share Posted September 9, 2008 16:00 is greater than 0:00, so the first if returns true, and it stops there... Link to comment https://forums.phpfreaks.com/topic/123373-solved-time-shows-correctly-text-not/#findComment-637240 Share on other sites More sharing options...
MrRock Posted September 9, 2008 Author Share Posted September 9, 2008 And it is solved how? Link to comment https://forums.phpfreaks.com/topic/123373-solved-time-shows-correctly-text-not/#findComment-637245 Share on other sites More sharing options...
MrRock Posted September 9, 2008 Author Share Posted September 9, 2008 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'; Link to comment https://forums.phpfreaks.com/topic/123373-solved-time-shows-correctly-text-not/#findComment-637246 Share on other sites More sharing options...
lanmonkey Posted September 9, 2008 Share Posted September 9, 2008 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 Link to comment https://forums.phpfreaks.com/topic/123373-solved-time-shows-correctly-text-not/#findComment-637350 Share on other sites More sharing options...
MrRock Posted September 9, 2008 Author Share Posted September 9, 2008 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? Link to comment https://forums.phpfreaks.com/topic/123373-solved-time-shows-correctly-text-not/#findComment-637458 Share on other sites More sharing options...
lanmonkey Posted September 9, 2008 Share Posted September 9, 2008 yeah you can remove that Link to comment https://forums.phpfreaks.com/topic/123373-solved-time-shows-correctly-text-not/#findComment-637478 Share on other sites More sharing options...
MrRock Posted September 9, 2008 Author Share Posted September 9, 2008 Thank you veryvery much Link to comment https://forums.phpfreaks.com/topic/123373-solved-time-shows-correctly-text-not/#findComment-637509 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.