How can I compare hours:minutes with time variable in PHP? - php

I wrote the following, however it doesn't seem to work.
What am I missing here?
Thank you
$ora_tora = date('H:i', strtotime('+2 hours'));
if (($ora_tora >= '00:00') && ($ora_tora < '02:00')) { echo '1'; }
else if (($ora_tora >= '02:00') && ($ora_tora < '04:00')) { echo '2'; }
else if (($ora_tora >= '04:00') && ($ora_tora < '06:00')) { echo '3'; }
else if (($ora_tora >= '06:00') && ($ora_tora < '08:00')) { echo '4'; }
else if (($ora_tora >= '08:00') && ($ora_tora < '10:00')) { echo '5'; }
else if (($ora_tora >= '10:00') && ($ora_tora < '12:00')) { echo '6'; }
else if (($ora_tora >= '12:00') && ($ora_tora < '14:00')) { echo '7'; }
else if (($ora_tora >= '14:00') && ($ora_tora < '16:00')) { echo '8'; }
else if (($ora_tora >= '16:00') && ($ora_tora < '18:00')) { echo '9'; }
else if (($ora_tora >= '18:00') && ($ora_tora < '20:00')) { echo '10'; }
else if (($ora_tora >= '20:00') && ($ora_tora < '22:00')) { echo '11'; }
else if (($ora_tora >= '22:00') && ($ora_tora < '00:00')) { echo '12'; }

You could replace all of that with this:
echo intdiv(date('G', strtotime('+2 hours')), 2) + 1;
'G' is the format for 24 hours without leading 0 (so from 0 to 23)

since you actually compare hours you can do:
$ora_tora = (int)date('G', strtotime('+2 hours'));
if ($ora_tora > 2 && $ora_tora < 4) {
}

Related

Trying to show different banner images based on server time using PHP

I have four greeting images which I intend to show based on what time user enters the site.
$morning = "img/morning.png";
$afternoon = "img/afternoon.png";
$evening = "img/evening.png";
$night = "img/night.png";
And I have some conditional statements to assign the values to $cover variable. When I tested, the conditional statement doesn't work.
date_default_timezone_set('Asia/Yangon');
$Hour = date('G');
if($Hour >= 5 && $Hour <= 11) {
$cover = $morning;
}elseif ($Hour >= 11 && $Hour <= 4) {
$cover = $afternoon;
}elseif ($Hour >= 4 && $Hour <= 9){
$cover = $evening;
}elseif ($Hour >= 9 && $Hour <= 4) {
$cover = $night;
}
Img tag
<img class="card-img-top" src="<?php echo $cover; ?>" alt="Not Available" >
if($Hour >= 0 && $Hour <= 11) {
$cover = $morning;
}
elseif ($Hour > 11 && $Hour <= 16) {
$cover = $afternoon;
}
elseif ($Hour > 16 && $Hour <= 19){
$cover = $evening;
}
else{
$cover = $night;
}
Above code will check your hours from 00:00 until 24:00 next day. I fixed your if-else statements so they make more sense in a way that there is a flow in the times.
G 24-hour format of an hour without leading zeros 0 through 23
$hour = date('H', time());
if( $hour > 6 && $hour <= 11) {
$cover = $morning;
}
else if($hour > 11 && $hour <= 16) {
$cover = $afternoon;
}
else if($hour > 16 && $hour <= 23) {
$cover = $evening;
}
else {
$cover = $night;
}
Construct an instance of date and acquire the hour from it

eliminate weekends (sunday) by a php script

I am trying to count number of working days available for particular hours set. here i just need to exclude Sundays by the following php script. if this script find a Sunday this should increase the count. its working but,
This script is capable to exclude first 'Sunday' but not the 'second' and 'third'.
kindly give me a solution to correct this
function testRange() {
$phone_Quantity = 0;
$phone_Quantity = $_POST['phoneQuantity'];
if ($phone_Quantity > 0 && $phone_Quantity <= 300) {
return 300;
} elseif ($phone_Quantity >= 301 && $phone_Quantity <= 600) {
return 600;
} elseif ($phone_Quantity >= 601 && $phone_Quantity <= 900) {
return 900;
} elseif ($phone_Quantity >= 601 && $phone_Quantity <= 1200) {
return 1200;
} elseif ($phone_Quantity >= 1201 && $phone_Quantity <= 1500) {
return 1500;
} elseif ($phone_Quantity >= 1501 && $phone_Quantity <= 1800) {
return 1800;
}
}
echo testRange();
$query_to_get_hours = "SELECT
cdma_filtering_target_hours.Target_hours
FROM
cdma_filtering_target_hours
WHERE
cdma_filtering_target_hours.No_of_units='" . testRange() . "'
";
$query_to_get_hours_query = $system->prepareSelectQuery($query_to_get_hours);
foreach ($query_to_get_hours_query as $THours) {
$targeted_hours = $THours['Target_hours'];
}
$hid = 24; // Hours in a day - could be 24, 48, etc
$days = round($targeted_hours / $hid);
for ($xdays = 0; $xdays < $days; $xdays++) {
if (date('l', strtotime(date('y-m-d', strtotime("+$xdays days")))) == 'Sunday') {
$days++;
break;
}
}
Why do you converting +$xdays string representation twice?
If you comment your if statement and add next line
echo date('l', strtotime("+$xdays days"));
you can clearly see that it works.

php - IF - ELSEIF- ELSE

I need to create a php that generates sentence depends on what time it is. If time is 5 - 8, echo "good morning" and so on...
Here's my code
<?php
if (date('G') <= 5 - 8)
{
echo "Morning";
}
elseif (date('G') = 9 - 12) {
echo "LOREM";
}
elseif (date('G') = 12) {
echo "LOREM";
}
elseif (date('G') = 12 - 17) {
echo "LOREM";
}
elseif (date('G') = 17 - 19) {
echo "LOREM";
}
else (date('G') >= 20) {
echo "NIGHT";
}
?>
What's wrong _?
5-8 is not "five through eight", it is "five minus eight". The - means subtraction. There is no "through" operator in PHP.
You had some contradictory statements in your example so mine doesn't perfectly match yours but this should point you in the right direction.
$hour= date('G');
if ($hour >= 5 && $hour <= 8) {
echo "Morning";
} elseif ($hour >= 9 && $hour < 12) {
echo "LOREM";
} elseif ($hour == 12) {
echo "LOREM";
} elseif ($hour > 12 && $hour < 17) {
echo "LOREM";
} elseif ($hour >= 17 && $hour <= 19) {
echo "LOREM";
} elseif ($hour >= 20) {
echo "NIGHT";
}
Use:
<?php
if (date('G') >= 5 && date('G') <= 8)
{
echo "Morning";
}
elseif (date('G') >= 9 && date('G') <= 11)
{
echo "LOREM";
}
elseif (date('G') == 12)
{
echo "LOREM";
}
elseif (date('G') >= 13 && date('G') <= 16)
{
echo "LOREM";
}
elseif (date('G') >= 17 && date('G') <= 19)
{
echo "LOREM";
}
elseif (date('G') >= 20)
{
echo "NIGHT";
}
?>

PHP displaying wrong image

I've just finished writing my script that changes an image depending on the time. It's all good except it's displaying the wrong image.
<?php
date_default_timezone_set('America/Los_Angeles');
$w = date('W'); # week
$d = date('N'); # day
$t = date('G'); # time
dealWithTime($d);
function dealWithTime($day) {
if ($w == 13) { # Week 13
if ($day == 1) {
# Monday
if ($t >= 0 && $t < 6) {
printImage('XD_Holo.png');
} else if ($t >= 6 && $t < 10) {
printImage('Midtown.png');
} else if ($t >= 10 && $t < 14) {
printImage('Terminal.png');
} else if ($t >= 14 && $t < 18) {
printImage('XD_Holo.png');
} else if ($t >= 18) {
printImage('Midtown.png');
}
} else if ($day == 2) {
# Tuesday
if ($t >= 0 && $t < 6) {
printImage('XD_Holo.png');
} else if ($t >= 6 && $t < 10) {
printImage('Midtown.png');
} else if ($t >= 10 && $t < 14) {
printImage('Terminal.png');
} else if ($t >= 14 && $t < 18) {
printImage('XD_Holo.png');
} else if ($t >= 18) {
printImage('Midtown.png');
}
} else if ($day == 3) {
# Wednesday
if ($t >= 0 && $t < 6) {
printImage('XD_Holo.png');
} else if ($t >= 6 && $t < 10) {
printImage('Midtown.png');
} else if ($t >= 10 && $t < 14) {
printImage('Terminal.png');
} else if ($t >= 14 && $t < 18) {
printImage('XD_Holo.png');
} else if ($t >= 18) {
printImage('Midtown.png');
}
} else if ($day == 4) {
# Thursday
if ($t >= 0 && $t < 6) {
printImage('XD_Holo.png');
} else if ($t >= 6 && $t < 10) {
printImage('Midtown.png');
} else if ($t >= 10 && $t < 14) {
printImage('Terminal.png');
} else if ($t >= 14 && $t < 18) {
printImage('XD_Holo.png');
} else if ($t >= 18) {
printImage('Midtown.png');
}
} else if ($day == 5) {
# Friday
if ($t >= 2 && $t < 8) {
printImage('Midtown.png');
} else if ($t >= 8 && $t < 12) {
printImage('Terminal.png');
} else if ($t >= 12 && $t < 16) {
printImage('XD_Holo.png');
} else if ($t >= 16 && $t < 20) {
printImage('Midtown.png');
} else if ($t >= 20) {
printImage('Terminal.png'); // SHOULD BE THIS ONE
}
} else if ($day == 6) {
# Saturday
if ($t >= 0 && $t < 6) {
printImage('XD_Holo.png');
} else if ($t >= 6 && $t < 10) {
printImage('Midtown.png');
} else if ($t >= 10 && $t < 14) {
printImage('Terminal.png');
} else if ($t >= 14 && $t < 18) {
printImage('XD_Holo.png');
} else if ($t >= 18 && $t < 22) {
printImage('Midtown.png');
} else if($t >= 22) {
printImage('Terminal.png');
}
} else if ($day == 7) {
# Sunday
if ($t >= 2 && $t < 8) {
printImage('XD_Holo.png');
} else if ($t >= 8 && $t < 12) {
printImage('Midtown.png');
} else if ($t >= 12 && $t < 16) {
printImage('Terminal.png');
} else if ($t >= 16 && $t < 20) {
printImage('XD_Holo.png');
} else if ($t >= 20) {
printImage('Midtown.png');
}
}
} else if ($w == 14) { # Week 14
if ($day == 1) {
# Monday
if ($t >= 0 && $t < 6) {
printImage('Terminal.png');
} else if ($t >= 6 && $t < 10) {
printImage('XD_Holo.png');
} else if ($t >= 10 && $t < 14) {
printImage('Midtown.png');
} else if ($t >= 14 && $t < 18) {
printImage('Terminal.png');
} else if ($t >= 18 && $t < 22) {
printImage('XD_Holo.png');
} else if ($t >= 22) {
printImage('Midtown.png');
}
} else if ($day == 2) {
# Tuesday
if ($t >= 2 && $t < 8) {
printImage('Terminal.png');
} else if ($t >= 6 && $t < 10) {
printImage('XD_Holo.png');
} else if ($t >= 10 && $t < 14) {
printImage('Midtown.png');
} else if ($t >= 14 && $t < 18) {
printImage('Terminal.png');
} else if ($t >= 18) {
printImage('XD_Holo.png');
}
} else if ($day == 3) {
# Wednesday
if ($t >= 0 && $t < 6) {
printImage('fin.png');
} else if ($t >= 6 && $t < 10) {
printImage('fin.png');
} else if ($t >= 10 && $t < 14) {
printImage('fin.png');
} else if ($t >= 14 && $t < 18) {
printImage('fin.png');
} else if ($t >= 18) {
printImage('fin.png');
}
} else if ($day == 4) {
# Thursday
if ($t >= 0 && $t < 6) {
printImage('fin.png');
} else if ($t >= 6 && $t < 10) {
printImage('fin.png');
} else if ($t >= 10 && $t < 14) {
printImage('fin.png');
} else if ($t >= 14 && $t < 18) {
printImage('fin.png');
} else if ($t >= 18) {
printImage('fin.png');
}
} else if ($day == 5) {
# Friday
if ($t >= 0 && $t < 6) {
printImage('fin.png');
} else if ($t >= 6 && $t < 10) {
printImage('fin.png');
} else if ($t >= 10 && $t < 14) {
printImage('fin.png');
} else if ($t >= 14 && $t < 18) {
printImage('fin.png');
} else if ($t >= 18) {
printImage('fin.png');
}
} else if ($day == 6) {
# Saturday
if ($t >= 0 && $t < 6) {
printImage('fin.png');
} else if ($t >= 6 && $t < 10) {
printImage('fin.png');
} else if ($t >= 10 && $t < 14) {
printImage('fin.png');
} else if ($t >= 14 && $t < 18) {
printImage('fin.png');
} else if ($t >= 18) {
printImage('fin.png');
}
} else if ($day == 7) {
# Sunday
if ($t >= 0 && $t < 6) {
printImage('fin.png');
} else if ($t >= 6 && $t < 10) {
printImage('fin.png');
} else if ($t >= 10 && $t < 14) {
printImage('fin.png');
} else if ($t >= 14 && $t < 18) {
printImage('fin.png');
} else if ($t >= 18) {
printImage('fin.png');
}
}
} else { # else
printImage('fin.png');
}
}
function printImage($im) {
$file = $im;
$type = 'image/png';
header('Content-Type:'.$type);
header('Content-Length: ' . filesize($file));
readfile($file);
}
?>
As you scroll down, you should see a comment saying what image it should be (as of this post).
I also wrote another script to see if it was the time that was wrong but it's giving me the correct results.
<?php
date_default_timezone_set('America/Los_Angeles');
$w = date('W'); # week
$d = date('N'); # day
$t = date('G'); # time
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<h2>Week:</h2>
<span><?php echo $w; ?></span>
<h2>Day:</h2>
<span><?php echo $d; ?></span>
<h2>Time:</h2>
<span><?php echo $t; ?></span>
</body>
</html>
The first script can be seen here: http://spedwards.cz.cc/event/e.php
And the second here: http://spedwards.cz.cc/event/t.php
I will not be changing these files so they will stay relevant to the question for at least a month.
Could someone please explain to me why the correct image doesn't show? All 4 images are in the same directory.
You're not passing the variables into the function call.
For example:
dealWithTime($w, $d, $t);
function dealWithTime($w, $day, $t) {
The return value of the date format functions are strings, you are trying to evaluate them as integers.
You should convert the variables to int using (int) $d before evaluating it.

php - how to check if a date is in a given season (regardless of the current year)?

High Season:
April 28 - September 30,
December 27 - January 3
Low Season:
October 1 - December 26,
January 4 - April 27
I gave a date to check: 2014-02-18 and I want to have TRUE or FALSE in case of which season includes it. How to do it regardless of the current year?
Try with simple date comparing:
function is_high_season($date) {
$md = gmdate('m-d', strtotime($date));
return
('03-28' <= $md && $md <= '09-30') ||
('12-27' <= $md && $md <= '12-31') ||
('01-01' <= $md && $md <= '01-03');
}
demo
$day = $d->format('j');
$month = $d->format('n');
if($month == 1 && $day <= 3) {
return 'high';
} elseif $month < 4 || ($month == 4 && $day < 28)) {
return 'low';
} elseif($month == 4 && $day >= 28) {
return 'high';
} elseif($month < 10) {
return 'high';
} elseif($month < 12 || ($month == 12 && $day < 27)) {
return 'low';
} elseif($month == 12 && $day >= 27) {
return 'high';
}
$Date = date('Y-m-d');
$Date=date('Y-m-d', strtotime($Date));;
//echo $Date; // echos today!
$hiDateBegin = date('Y-m-d', strtotime("28/04/2014"));
$hiDateEnd = date('Y-m-d', strtotime("30/09/2014"));
$hiDateBegin2 = date('Y-m-d', strtotime("27/12/2014"));
$hiDateEnd2 = date('Y-m-d', strtotime("03/01/2015"));
$lowDateBegin = date('Y-m-d', strtotime("01/10/2014"));
$lowDateEnd = date('Y-m-d', strtotime("26/12/2014"));
$lowDateBegin2 = date('Y-m-d', strtotime("04/01/2015"));
$lowDateEnd2 = date('Y-m-d', strtotime("27/04/2015"));
if (($Date > $hiDateBegin) && ($Date < $hiDateEnd))
{
echo "is Hi Season";
}
if (($Date > $hiDateBegin2) && ($Date < $hiDateEnd2))
{
echo "is Hi Season";
}
if (($Date > $lowDateBegin) && ($Date < $lowDateEnd))
{
echo "is Low Season";
}
if (($Date > $lowDateBegin2) && ($Date < $lowDateEnd2))
{
echo "is Low Season";
}
else
{
echo "Date doesn't fall in a season!";
}

Categories