PHP Display link between specific time - php

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';
}

Related

Why doesn't subtraction sign work on while loop in php?

<?php
$hp = 0;
while($hp < 50) {
$flip = rand(0,2);
if ($flip == 1) {
echo "<p>X-Ray</p>";
$hp += 15;
} elseif ($flip == 2) {
echo "<p>Special Move</p>";
$hp += 10;
} else {
echo "<p>Punch</p>";
$hp += 5;
}
echo "<p>Total so far: $hp</p>";
echo "</br>";
}
?>
This is a PHP code. When I run it, it works fine. However, when I change it to this code below it doesn't.
<?php
$hp = 50;
while($hp > 1) {
$flip = rand(0,2);
if ($flip == 1) {
echo "<p>X-Ray</p>";
$hp -= 15;
} elseif ($flip == 2) {
echo "<p>Special Move</p>";
$hp -= 10;
} else {
echo "<p>Punch</p>";
$hp -= 5;
}
echo "<p>Total so far: $hp</p>";
echo "</br>";
}
?>
Please help. tHE CHANGES I MADE ARE THE HIGHLIGHTED ONES.
You never created $hp properly in the second version:
50;
doesn't do anything. It just tells php "here, have a 50", and php goes "gee, thanks, ok, whatever" and moves onwards. Then you have
while($hp > 1) {
Since $hp is undefined, it's null, and the code parses/executes as:
while($hp > 1) {
while(null > 1) {
while(0 > 1) {
FALSE -> exit loop
You never created $hp properly in the second version:
50;
If you do change it to $hp = 50;

PHP Change background on Time

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 ;-)

Count how many times strpos accured inside a foreach loop

How can i show how many times a sence was shown while looping?
$i = 0;
foreach ($parts as $new[$i]) {
$abouttoexpire = strpos($new[$i], 'Your Airbnb question is about to expire');
$anairbnbexpert = strpos($new[$i], 'An Airbnb expert is waiting on feedback from you regarding');
$requesttoalter = strpos($new[$i], 'Your request to alter reservation');
//when the message was made
preg_match('/<div class="timestamp"[\s\S]*?>(.*)*?\+0000<\/div>/', $new[$i], $dateofmsg);
//between 9am and 6pm
$hour = date('H', strtotime(#$dateofmsg[1]));
if ($hour >= 9 && $hour <= 18) {
//if we found this sentence
if ($abouttoexpire !== FALSE) {
//how i show here how many times we catch the sentence #abouttoexpire??
}
}
}
$i++;
Inside the condition: if ($abouttoexpire !== FALSE)
How do i print the times this sence (#abouttoexpire) was accured during the loop?
Try this...
$i = 0;
$j = 0;
$k = 0;
foreach ($parts as $new[$i]) {
if ($hour >= 9 && $hour <= 18) {
$j++;
if ($abouttoexpire !== FALSE) {
$k++;
}
}
$i++;
}
echo "foreach loop=".$i;
echo "if loop =".$j;
echo "within if loop =".$k;
$i = 0;$j=0;
foreach ($parts as $new[$i]) {
$abouttoexpire = strpos($new[$i], 'Your Airbnb question is about to expire');
$anairbnbexpert = strpos($new[$i], 'An Airbnb expert is waiting on feedback from you regarding');
$requesttoalter = strpos($new[$i], 'Your request to alter reservation');
//when the message was made
preg_match('/<div class="timestamp"[\s\S]*?>(.*)*?\+0000<\/div>/', $new[$i], $dateofmsg);
//between 9am and 6pm
$hour = date('H', strtotime(#$dateofmsg[1]));
if ($hour >= 9 && $hour <= 18) {
//if we found this sentence
if ($abouttoexpire !== FALSE) {
$j++;
}
}
}
$i++;
echo 'no_of_times:'.$j;

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?

Swap div visibility based on time / schedule

On my page I have two divs... one div I'd like to be visible from 10am to 6pm ( server time ).... and the other div for the remaining hours.
I tried a bunch of searches to find some sort of a javascript or jquery content swapper without any luck.. thanks for any suggestions?
<div id="day">content</div>
<div id="night">content</div>
I was able to get this working using only the following php:
<?php
$hour = strftime("%H");
if ($hour >= 02 && $hour < 05)
{
echo '<div id="div1">content block one </div>';
}
else
{
echo '<div id="div2">content block two</div>';
}?>
However this solution doesn't seem to work if I want to show the div from 8pm until 4am... is this because it is spanning more than one day? Thanks for any suggestions.
EDIT
The case you mentioned in your comment is a thorny one. So here is my revised-revised answer:
<?php
$t0 = 20; // from hour (inclusive) -- int, 0-23
$t1 = 4; // till hour (excluding) -- int, 0-23
$t = date('G'); // current hour (derived from current time) -- int, 0-23
if ($t0 == $t1) {
$in_range = NULL;
} elseif ($t0 < $t1) {
$in_range = ($t0 <= $t && $t < $t1);
} else {
$in_range = ($t1 <= $t && $t < $t0) == false;
}
/*
echo $in_range === NULL
? 'from and till dates must be different'
: ($in_range ? 'just in time' : 'wait till the time is right');
*/
if ($in_range === false) {
$s0 = mktime($t0, 0, 0); // lunch time
$s = time(); // current time
if ($s0 < $s) {
$s0 += 60 * 60 * 24; // late for lunch! now wait till tomorrow
}
$d0 = $s0 - $s;
$dh = floor($d0 / 60 / 60);
$dm = $d0 - $dh * 60 * 60; $dm = floor($dm / 60);
$ds = $d0 - $dh * 60 * 60 - $dm * 60;
echo sprintf("
Current date...: %s<br />
Target date....: %s<br />
Time to go.....: %d hours, %d minutes, %d seconds
",
date("Y-m-d h:i:s A", $s),
date("Y-m-d h:i:s A", $s0),
$dh,
$dm,
$ds
);
}
?>
If you need to set visibility based on server time, you should show and hide the divs from the server side.
Because no JavaScript can get the server time but the client (possible remote located) browser.
If you have a script in your site that can return the server time, then you could achieve this, but I think it would be more sensible to mark the HTML (php, jsp, whatever) code conditionally from scratch.
Just use $(document).ready() handler with Date() (eg. getHours() method) as I did with the following code: jsfiddle.net/c2TPZ. You can hide both blocks by default unless your JS sets CSS styles for them to be visible, eg. like this:
$('#box1').css({display: 'block'}); // sets display to 'block', eg. from 'none'
I would generally recommend this be done on the server, but it would look something like this in Javascript:
var pHour = Date.getHours();
if ((pHour >= 10) && (pHour < 18))
{
$('.scheduled-target').show();
}
This assumes that you'd make '.scheduled-target' display:none by default.
Also this is for the client's local time. If you want an absolute time, you'll want to start with Date.getUTCHours() and do offsets for your Timezone/Locale.
$(document).ready(function() {
var d = new Date();
if (d.getHours() >= 10 && d.getHours() < 18) {
$(#div1).show();
$(#div2).hide();
}
else {
$(#div1).hide();
$(#div2).show();
}
});
EDIT: Oops, I missed the part about server time. This won't be reliable. Instead you should set them with server-side scripting. Don't know what you're using on the server side, but for example in PHP:
$hour = strftime("%H");
if ($hour >= 10 && $hour < 18)
{
$div1_class = "show";
$div2_class = "hide";
}
In your css, class .hide is display: none;
Theoritically it is as simple as this:
HTML
<div class="h h10-6">10-6</div>
<div class="h h-other">other time of day</div>
JavaScript
current_hour = <? some_server_side_code_to_get_the_hour ?>;
if(current_hour >= 10 && current_hour < 18) $('.h10-6').show();
else $('.h-other').show();
CSS
.h { display: none; }
I used this
<?php
date_default_timezone_set("America/Los_Angeles");
$rightNow = date("m:d:h:i:sa");
//echo $rightNow ;
//$startHide = date("6:11:02:30:00pm")
$startHide = date("06:11:02:30:00pm");
$endHide = date("06:12:05:00:00pm");
?>
<? if ($rightNow > $startHide && $rightNow < $endHide): ?>
<style> .hideCertainTimes {
display:none !important;
}
</style>
<? else: ?>
<? endif; ?>

Categories