PHP Change background on Time - php

So, For school i have to make PHP show the time, And for example when it is 12:00 the background is a afternoon one, and on 02:00 it is a Night one. This is my code:
<body>
<?php
<div class="tijd">
date_default_timezone_set('GMT+1');
echo date('h:i');
$Tijd = date('h');
if ($Tijd > 12 || $Tijd <17') {
echo '<div class="Middag"> </div>';
}
if ($Tijd > 12 || $Tijd <17') {
echo '<div class="Avond"> </div>';
}
if ($Tijd >= '22') {
echo '<div class="Nacht"> </div>';
}
if ($Tijd >= '6') {
echo '<div class="Ochtend"> </div>';
}
echo('Dit is een test...')
?>
</div>
</body>
</html>
But, What is not working here? On the webpage it shows the time correctly but it doesnt show the background. It is also not working while i do a background color or something so i know it is not only the background color. I tried also to make a background in PHP but i kind of failed at that.

You should use if, else if:
if ($Tijd > 12 || $Tijd <17) {
echo '<div class="Middag"> </div>';
} else if ($Tijd > 12 || $Tijd <17) {
echo '<div class="Avond"> </div>';
} else if ($Tijd >= 22) {
echo '<div class="Nacht"> </div>';
} else if ($Tijd >= 6) {
echo '<div class="Ochtend"> </div>';
}
Having said that, your first two test conditions are exactly the same, so you should look at that too

(this should be a comment but its a bit long).
In addition to Marc B's comment, there's random quotes all over the place - don't quote numeric values when you're trying to do a numeric comparison. You're mixing HTML and PHP -
<?php
<div class="tijd">
This should be causing your code to throw big errors. If you're not seeing these errors then you need to investigate why.
And the way you are running multiple if statements is messy. You could use if...else if...else if ...else, but if you use a switch statement your code will be much clearer:
switch((integer)$Tijd) {
case 13:
case 14:
case 15:
case 16:
echo '<div class="Middag"> </div>';
break;
case 22:
case 23:
echo '<div class="Nacht"> </div>';
break;
default:
echo '<div class="Avond"> </div>';
break;
}
As you can see - there are gaps here which are not described by your original code.

<?php
$hour = date('H'); //H is for 24 hours interval
if ($hour > 4 && $hour < 6) {
$class = 'earlier-morning';
} elseif ($hour >=6 && $hour <=11) {
$class = 'morning';
} elseif ($hour >=11 && $hour < 15) {
$class = 'midday';
} elseif($hour >= 15 && $hour < 19) {
$class = 'day';
} elseif ($hour >= 19 && $hour < 22) {
$class = 'evening';
//the only one case left - hours between 22 and 4
} else {
$class = 'night';
}
echo sprintf('<div class="%s"></div>', $class);
The trick here is elseif
So only ONE condition will always work here.
P.S. I believe that extra quotes in your example is a typo ;-)

Related

How to show grade according to marks in php?

code:
<?php
if($outoff!=0)
{
$grade = ($score/$outoff)*100;
if($grade <= 39)
{
echo '<span class="text-danger">Bad</span>';
}
else if($grade >=74)
{
echo '<span class="text-warning">Average</span>';
}
else if($grade >=100)
{
echo '<span class="text-success">Good</span>';
}
}
else
{
//no comment please
}
?>
Show grade according to:-
0-39 (Bad)
40-74 (Average)
75-100 (Good)
In this question I want to show message bad, average, good according to grade. Suppose if grade is 0-39 then it will show bad similarly if grade is 40-74 then show average like this but the condition I am giving is wrong. So, how can I do it?
Just change greater than to less than.
<?php
if($outoff!=0)
{
$grade = ($score/$outoff)*100;
if($grade <= 39)
{
echo '<span class="text-danger">Bad</span>';
}
else if($grade <=74) //Change to less than here.
{
echo '<span class="text-warning">Average</span>';
}
else if($grade <=100) //Change to less than here.
{
echo '<span class="text-success">Good</span>';
}
}
else
{
//no comment please
}
You need to modify the conditions so that no score is missed out of grade.
So, please define 3 ranges of scores using if and `else if'.
Range 1: 0-39: if ($grade <= 39) {
Range 2: 40-74: else if($grade <=74) {
Range 3: 75-100: else if($grade <=100) {
This way, first if checks if the grade is less than or equal to 39.
If yes, grade is Bad.
Else, if score, does not fit in this range, it will go ahead in next if else for the range: 40-74 and same way to 75-100 if it does not fit.
Corrected code:
if ($outoff!=0) {
$grade = ($score/$outoff)*100;
if ($grade <= 39) { // Score range: 0-39
echo '<span class="text-danger">Bad</span>';
}
// If $score is coming to this else if means it is definitely
// greater than 39: that is 40+
// Score range: 40-74 as it is in else if after if of `39`
else if($grade <=74) {
echo '<span class="text-warning">Average</span>';
}
// Score range: 75-100 as it is in else if after 0 - 39 and 40 - 74
else if($grade <=100) {
echo '<span class="text-success">Good</span>';
}
}
You have to make changes to you code as follow:
<?php
if($outoff!=0)
{
$grade = ($score/$outoff)*100;
if( $grade >= 0 && $grade < 40 ) {
echo '<span class="text-danger">Bad</span>';
}
else if( $grade > 39 && $grade < 75 ) {
echo '<span class="text-warning">Average</span>';
}
else if($grade > 74 && $grade <= 100 ) {
echo '<span class="text-success">Good</span>';
}
}
else
{
//no comment please
}
?>
<?php
if($outoff!=0)
{
$grade = ($score/$outoff)*100;
if( $grade > 0 && $grade <= 39 ) {
echo '<span class="text-danger">Bad</span>';
}
else if( $grade >= 40 && $grade <= 74 ) {
echo '<span class="text-warning">Average</span>';
}
else if($grade >= 75 && $grade <= 100 ) {
echo '<span class="text-success">Good</span>';
}
}
else
{
//no comment please
}
?>

Echo Out Interger Differently If More Than Value PHP

So I'm creating a points system for my website which I want to change to an echo instead of the actual integer when shown on the users profile.
For example: When the integer is lower than 1000 it displays as the actual number (lets say: 645). But when it is between 1000 and 1100, it will display as '1k' and so on. What I've got so far does work, but displays incorrectly and does seem a bit of a waste of space.. Is there any way to do this in a much simpler; faster way?
Thanks!
code:
<?php
$points_disp = $user_data['points'];
if($points_disp < 1000){
echo $points_disp;
} else if ($points_disp >= 1000){
echo '1k';
} else if ($points_disp >= 1200){
echo '1.2k';
} else if ($points_disp >= 1400){
echo '1.4k';
} else if ($points_disp >= 1600){
echo '1.6k';
} else if ($points_disp >= 1800){
echo '1.8k';
} else if ($points_disp >= 2000){
echo '2k';
}
?>
Edit: I figured out an easier way to do this;
code (for anyone else who needs to do this):
<?php
$points_disp = $user_data['points'];
$fdigit = substr($points_disp, 0, 1);
$sdigit = substr($points_disp, 1, 1);
if ($points_disp < 1000){
echo $points_disp;
} else if ($points_disp >= 1000){
echo $fdigit . "." . $sdigit . "k";
}
echo $num;
?>
You can use switch case:
$points_disp = $user_data['points'];
switch(true)
{
case ($points_disp < 1000):
$num = $points_disp;
break;
case ($points_disp > 1000 && $points_disp < 1100 ):
$num = '1.2k';
break;
//...so on
}
echo $num;
Try this,
if($points_disp < 1000){
echo $points_disp;
} else if($points_disp >= 1000) {
echo round($points_disp/1000,1) . "K";
}

TimeFrame Statistic Issue

I'm having a problem regarding a time frame statistic as you see here: http://www.ivao.ch/rfe_gva/stats . I don't know why but from 1200 to 1300z I've all the flight and this is not correct..I wanna see only the flight for that time..
<?php
for ($i=8;$i<18;$i++) {
if ($i % 2 == 0) {
echo "<div class=\"row margintop20\">";
}
if ($i >= 24) {
$time = ($i-24)*100;
} else {
$time = $i*100;
}
$time100 = $time+100;
?>
How can i solve this problem?

using if statement in mysql_fetch_array

I have the following query. I want to show the time if number of Adults is less than 10.
$result = mysql_query("SELECT sum(Adult) AS Number1, Time FROM orders WHERE confirmed = 'Y' and BookDate = '2013-04-01' GROUP BY Time");
while($row = mysql_fetch_array($result)) {
if ($row['Number1'] < '10')
echo '<div class="result">'.$row['Time'].' ('.$row['Number1'].')</div>';
}
so it will come out like this...
<div class="result">18:00:00 (8)</div>
<div class="result">18:30:00 (5)</div>
<div class="result">19:00:00 (6)</div>
However I also want to show that 18:15:00 has 0 Adults.
I have tried adding some if statement like this
if (($row['Time'] == '18:15:00') and ($row['Number1'] < '10')) {
echo '<div class="result">'.$row['Time'].' ('.$row['Number1'].')</div>';
}
else {
echo '<div class="result">18:15:00 (0)</div>';
}
but it keeps looping. Can someone show me where I am going wrong?
If you want to break out of the loop statement, you can use the break statement:
while(condition){
if(conditionMet){
break;
}
}
I'm not really sure what you're trying to do, but maybe this is it:
if ($row['Number1'] < '10') {
echo '<div class="result">'.$row['Time'].' ('.$row['Number1'].')</div>';
} elseif ($row['Time']) == '18:15:00') {
echo '<div class="result">18:15:00 (0)</div>';
}
Change:-
if (($row['Time'] == '18:15:00') and ($row['Number1'] < '10'))
{
echo '<div class="result">'.$row['Time'].' ('.$row['Number1'].')</div>';
}
else
{
echo '<div class="result">18:15:00 (0)</div>';
}
to
if ($row['Number1'] < '10')
{
echo '<div class="result">'.$row['Time'].' ('.$row['Number1'].')</div>';
}
elseif(($row['Time']) == '18:15:00')
{
echo '<div class="result">18:15:00 (0)</div>';
}

PHP Display link between specific time

I need to display a link at a very specific time using PHP. The php for the WLSG schedule works and that's because it's a pretty simple program that starts on the hour and ends on the hour.
My dilemma is that I have another program that starts at (server time) 23:45 on Sunday and ends at 0:00 on Monday. I'm having issues with it displaying the entire day regardless of the times I've entered here. I've been tinkering with it for the last hour and just cannot figure out what I'm missing.
Here is my php code:
<?php
// Variables
$h = date('G');
$m = date('i');
$d = date('l');
//WLSG Schedule
if ($d != 'Monday') $wlsgDayToggle = 'radio-offline';
if ($h > 0) $wlsgTimeToggle = 'radio-online';
if ($h < 1) $wlsgTimeToggle = 'radio-online';
else $wlsgTimeToggle = 'radio-offline';
//UCR Schedule
if ($d != 'Sunday') $ucrDayToggle = 'radio-offline';
if ($h > 23) $ucrTimeToggle = 'radio-online';
if ($m > 40) $ucrTimeToggle = 'radio-online';
if ($m < 59) $ucrTimeToggle = 'radio-online';
else $ucrTimeToggle = 'radio-offline';
?>
Here is the HTML:
<div id="radio-online">
<p><a class="<?php echo $wlsgDayToggle; ?> <?php echo $wlsgTimeToggle; ?>" href="#" title="Online! Listen Now!" target="_blank">Online! Listen Now!</a></p>
<p><a class="<?php echo $ucrDayToggle; ?> <?php echo $ucrTimeToggle; ?>" href="#" title="Online! Listen Now!" target="_blank">Online! Listen Now!</a></p>
</div>
And the CSS:
a.radio-online { display: inline; }
a.radio-offline { display: none; }
If you want to "string together the if statements", as you have just told me in your last comment, you must do (for example):
if (condition) {
// code
} else if (condition) {
// code
} else if (condition) {
// code
} else {
// code
}
But, if you need to display the link between 11:45pm and 11:59pm on Sunday, you could do something simpler:
if ($d == 'Sunday' && $h == 23 && $m >= 45 && $m <= 59) {
$ucrTimeToggle = 'radio-online';
} else {
$ucrTimeToggle = 'radio-offline';
}

Categories