Short version: I'm looking to create multiple conditions in an if/then statement which all need to be met. One of those conditions is an array.
Problem: It's ignoring my final condition/array.
Long story: I run a game with a strange calendar system, seen : http://www.beqanna.com/forum/bqcalendar.php. I'm trying to have something on the left side where it says "current season" spit out the current season. I'm having a lot of problems with it understanding the "month".
$d = date('d');
$m = date('m');
$season = '';
If ($d > '1' && $d < '15' && ($m == '01' or '03' or '05' or '07' or '09' or '11')) {
$season = 'Winter';
} If ($d > '16' && $d < '18' && ($m == '01' or '03' or '05' or '07' or '09' or '11')) {
$season = 'Spring';
} If ($d > '19' && $d < '23' && ($m == '01' or '03' or '05' or '07' or '09' or '11')) {
$season = 'Spring, Birthing';
} If ($d > '24' && $d < '31' && ($m == '01' or '03' or '05' or '07' or '09' or '11')) {
$season = 'Spring';
} If ($d > '1' && $d < '15' && ($m == '02' or '04' or '06' or '08' or '10' or '12')) {
$season = 'Summer';
} If ($d > '16' && $d < '18' && ($m == '02' or '04' or '06' or '08' or '10' or '12')) {
$season = 'Autumn';
} If ($d > '19' && $d < '23' && ($m == '02' or '04' or '06' or '08' or '10' or '12')) {
$season = 'Autumn, Breeding';
} If ($d > '24' && $d < '31' && ($m == '02' or '04' or '06' or '08' or '10' or '12')) {
$season = 'Autumn';
}
Other things I've tried -
$m == array (01,03,05,07,09)
$m == (%2) - for the even number months
I've tried moving my ' around the entire thing, adding and subtracting ( ). My final conclusion is that - for whatever reason - it isn't "listening" to the month. Like right now it should say "winter" but it keeps reading "Summer," which is the 1-15 date (correct) but wrong month.
Any suggestions?
First of all your first expression will always return true.
&& ($m == '01' or '03' or '05' or '07' or '09' or '11')
// $m == 1 OR tru or true... and becomes false or true or true
You could use in_array.
Here is the code you need.
<?php
$d = (int)date('d');
$m = (int)date('m');
$season = '';
//echo $d . ' ' . $m . PHP_EOL;
$winterMonths = [1,3,5,7,9,11];
$summerMonths = [2,4,6,8,10,12];
If ($d > 1 && $d < 15 && in_array($m,$winterMonths)) {
$season = 'Winter';
} else if ($d > 16 && $d < 18 && in_array($m,$winterMonths)) {
$season = 'Spring';
} else if ($d > 19 && $d < 23 && in_array($m,$winterMonths)) {
$season = 'Spring, Birthing';
} else if ($d > 24 && $d < 31 && in_array($m,$winterMonths)) {
$season = 'Spring';
} else if ($d > 1 && $d < 15 && in_array($m,$summerMonths)) {
$season = 'Summer';
} else if ($d > 16 && $d < 18 && in_array($m,$summerMonths)) {
$season = 'Autumn';
} else if ($d > 19 && $d < 23 && in_array($m,$summerMonths)) {
$season = 'Autumn, Breeding';
} else if ($d > 24 && $d < 31 && in_array($m,$summerMonths)) {
$season = 'Autumn';
}
echo $season;
Hope it helps.
You've forgot the else statement.
And you currently check if the date is above 1 and under 15. So your routine won't work for dates like 15, 16, 18, 19, 23, 24, 31.
You have to change the operator > to >=, so you will check if the date is above OR equal to the if.
For example your old code:
if ($d > 1 && ....)
If your date is 1, it won't go into this if.
New code:
if ($d >= 1 && ....)
If your date is 1, it will go into this if.
Here the complete routine:
$d = date('d');
$m = date('m');
$season = '';
$winterMonths = array(1,3,5,7,9,11);
$summerMonths = array(2,4,6,8,10,12);
if ($d >= 1 && $d <= 15 && in_array($m, $winterMonths)) {
$season = 'Winter';
} else if ($d >= 16 && $d <= 18 && in_array($m, $winterMonths)) {
$season = 'Spring';
} else if ($d >= 19 && $d <= 23 && in_array($m, $winterMonths)) {
$season = 'Spring, Birthing';
} else if ($d >= 24 && $d <= 31 && in_array($m, $winterMonths)) {
$season = 'Spring';
} else if ($d >= 1 && $d <= 15 && in_array($m, $summerMonths)) {
$season = 'Summer';
} else if ($d >= 16 && $d <= 18 && in_array($m, $summerMonths)) {
$season = 'Autumn';
} else if ($d >= 19 && $d <= 23 && in_array($m, $summerMonths)) {
$season = 'Autumn, Breeding';
} else if ($d >= 24 && $d <= 31 && in_array($m, $summerMonths)) {
$season = 'Autumn';
}
Hope this helps.
Related
I'm trying to identify the current work shift from 3 options (24 hour format)
First shift 06:00 to 13:59
Second shift 14:00 to 21:59
Third shift 22:00 to 05:59
I tried this, but it's not working as expected
$hour = date("0500");
$shift;
if ($hour >= 0600 && $hour <= 1359 ) {
$shift = 1;
}else if($hour >= 14 && $hour <= 2159 )
{
$shift = 2;
}else
{
$shift = 3;
}
Maybe:
$hour = data('H');
if($hour >= 6 && $hour < 14) {
$shift = 1;
} else if($hour >= 14 && $hour < 22) {
$shift = 2;
} else {
$shift = 3;
}
You could try something like this perhaps:
$hour = data('H');
switch( true ){
case ( $hour >= 6 && $hours < 14 ):$shift=1; break;
case ( $hour >=14 && $hour < 22 ):$shift=2; break;
default: $shift=3; break;
}
Put your time in quotes otherwise starting with 0 makes it an octal number
Also stick with one format if you are going to be comparing
$hour = date("Hi", strtotime("05:00"));
$shift;
if ($hour >= "0600" && $hour <= "1359" ) {
$shift = 1;
}else if($hour >= "1400" && $hour <= "2159" ) {
$shift = 2;
}else {
$shift = 3;
}
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.
The following code works to echo "Open" or "Closed" if time is between 8:15am and 5:30pm. I am trying to make it day specific. How can I incorporate format character 'D' as example, Mon hours 8:15am - 5:30pm .. echo "Open", Sat hours 8:15am - 1:00pm "Open". I want to be able to control echo of Open/Closed by each day and time.
current working code for hours only
<?php
date_default_timezone_set('America/New_York');
$hour = (int) date('Hi');
$open = "yah hoo, we are open";
$closed = "by golly, im closed";
if ($hour >= 0815 && $hour <=1735) {
// between 8:15am and 5:35pm
echo "$open";
} else {
echo "$closed";
}
?>
example of what I am trying to do:
$hour = (int) date('D Hi');
if ($hours >= 0815 && $hour <=1735 && $hour === 'Mon')
{ echo "$open"; }
else { echo "$closed"; }
if ($hours >= 0815 && $hour <=1300 && $hour === 'Sat')
{ echo "$open"; }
else { echo "$closed"; }
another example per The One and Only's answer which looks close to what I am looking for, but this also does not work
<?php
$openDaysArray = array('Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat','Sun');
$thisDate = date('D Hi');
$explode = explode(" ", $thisDate);
$day = $explode[0];
$time = $explode[1];
if (in_array($day, $openDaysArray))
if ($time < 815 || $time > 1730 && $day === 'Mon');
if ($time < 815 || $time > 1730 && $day === 'Tue');
if ($time < 815 || $time > 1730 && $day === 'Wed');
if ($time < 815 || $time > 1730 && $day === 'Thu');
if ($time < 815 || $time > 1730 && $day === 'Fri');
if ($time < 815 || $time > 1730 && $day === 'Sat');
if ($time < 815 || $time > 1730 && $day === 'Sun');
{echo 'Open';}
else {echo 'Closed';}
?>
I'd handle it this way. Set up an array of all your open times. If you know you're closed on Saturday and Sunday, there's really no need to proceed with with checking times at that point, so kill the process there first. Then simply find out what day of the week it is, look up the corresponding opening and closing times in your $hours array, create actual DateTime objects to compare (rather than integers). Then just return the appropriate message.
function getStatus() {
$hours = array(
'Mon' => ['open'=>'08:15', 'close'=>'17:35'],
'Tue' => ['open'=>'08:15', 'close'=>'17:35'],
'Wed' => ['open'=>'08:15', 'close'=>'17:35'],
'Thu' => ['open'=>'08:15', 'close'=>'22:35'],
'Fri' => ['open'=>'08:15', 'close'=>'17:35']
);
$now = new DateTime();
$day = date("D");
if ($day == "Sat" || $day == "Sun") {
return "Sorry we're closed on weekends'.";
}
$openingTime = new DateTime();
$closingTime = new DateTime();
$oArray = explode(":",$hours[$day]['open']);
$cArray = explode(":",$hours[$day]['close']);
$openingTime->setTime($oArray[0],$oArray[1]);
$closingTime->setTime($cArray[0],$cArray[1]);
if ($now >= $openingTime && $now < $closingTime) {
return "Hey We're Open!";
}
return "Sorry folks, park's closed. The moose out front should have told ya.";
}
echo getStatus();
Use a switch statement:
$thisDate = date('D Hi');
$hoursOfOpArray = array("Mon_Open" => "815", "Mon_Close" => "1730", "Tue_Open" => "815", "Tue_Close" => "1730"); //repeat for all days too fill this array
$explode = explode(" ", $thisDate);
$day = $explode[0];
$time = (int)$explode[1];
switch($day) {
case "Sun":
$status = "Closed";
break;
case "Mon":
$status = ($time < $hoursOfOpArray[$day . "_Open"] || $time > $hoursOfOpArray[$day . "_Close"]) ? "Closed" : "Open";
break;
//same as Monday case for all other days
}
echo $status;
This should also work:
echo ($day === 'Sun' || ($time < $hoursOfOpArray[$day . "_Open"]) || ($time > $hoursOfOpArray[$day . "_Close"])) ? "Closed" : "Open";
$o = ['Mon' => [815, 1735], /*and all other days*/'Sat' => [815, 1300]];
echo (date('Hi')>=$o[date('D')][0] && date('Hi')<=$o[date('D')][1]) ? "open": "closed";
Done! And dont ask.
This one works, added remarks to explain as much as possible.
<?php
date_default_timezone_set('America/New_York');
// Runs the function
echo time_str();
function time_str() {
if(IsHoliday())
{
return ClosedHoliday();
}
$dow = date('D'); // Your "now" parameter is implied
// Time in HHMM
$hm = (int)date("Gi");
switch(strtolower($dow)){
case 'mon': //MONDAY adjust hours - can adjust for lunch if needed
if ($hm >= 0 && $hm < 830) return Closed();
if ($hm >= 830 && $hm < 1200) return Open();
if ($hm >= 1200 && $hm < 1300) return Lunch();
if ($hm >= 1300 && $hm < 1730) return Open();
if ($hm >= 1730 && $hm < 2359) return Closed();
break;
case 'tue': //TUESDAY adjust hours
if ($hm >= 830 && $hm < 1730) return Open();
else return Closed();
break;
case 'wed': //WEDNESDAY adjust hours
if ($hm >= 830 && $hm < 1730) return Open();
else return Closed();
break;
case 'thu': //THURSDAY adjust hours
if ($hm >= 830 && $hm < 1730) return Open();
else return Closed();
break;
case 'fri': //FRIDAY adjust hours
if ($hm >= 830 && $hm < 1730) return Open();
else return Closed();
break;
case 'sat': //Saturday adjust hours
return Closed();
break;
case 'sun': //Saturday adjust hours
return Closed();
break;
}
}
// List of holidays
function HolidayList()
{
// Format: 2009/05/11 (year/month/day comma seperated for days)
return array("2016/11/24","2016/12/25");
}
// Function to check if today is a holiday
function IsHoliday()
{
// Retrieves the list of holidays
$holidayList = HolidayList();
// Checks if the date is in the holidaylist - remove Y/ if Holidays are same day each year
if(in_array(date("Y/m/d"),$holidayList))
{
return true;
}else
{
return false;
}
}
// Returns the data when open
function Open()
{
return 'Yes we are Open';
}
// Return the data when closed
function Closed()
{
return 'Sorry We are Closed';
}
// Returns the data when closed due to holiday
function ClosedHoliday()
{
return 'Closed for the Holiday';
}
// Returns if closed for lunch
// if not using hours like Monday - remove all this
// and make 'mon' case hours look like 'tue' case hours
function Lunch()
{
return 'Closed for Lunch';
}
?>
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!";
}
I'm using PHP to schedule images to come up on a website at a specific time. I'm fine until a picture has to come up say at 9:42am and end at 10:42am. a can get top of hours and partial hours ending at top of hour but I can't figure out how to schedule multiple partial hours in a row. This is what I'm using
date_default_timezone_set('America/Los_Angeles');
$h = date('G');
$m = date('i');
$d = date('w');
$year = date('Y');
// MONDAY SCHEDULE
if ($d == 1 && $h >= 0 && $h < 1) { $img = 'opendoor.jpg'; }
else if ($d == 1 && $h >= 13 && $h < 14) { $img = 'newtime.jpg'; }
else if ($d == 1 && $h >= 14 && $h < 15) { $img = 'weekend'; }
else if ($d == 1 && $h >= 15 && $h < 16) { $img = 'today.jpg'; }
else if ($d == 1 && $h >= 16 && $h <= 17 && $m >= 0 && $m < 30) { $img = 'walk.jpg'; }
else if ($d == 1 && $h >= 17 && $h < 19 && $m > 30) { $img = 'new.jpg'; }
else if ($d == 1 && $h >= 19 && $h < 20 && $m < 30) { $img = 'default.jpg'; }
else if ($d == 1 && $h >= 19 && $h < 20 && $m > 30) { $img = 'test.jpg'; }
I followed a very good example given to me for my question, but for no apparent reason, some of the hours work and some doesn't. When I echo $img on one hour the code is printed but on the hour before or after it is ommitted. Here is a partial code of what I'm now doing:
date_default_timezone_set('America/Los_Angeles');
$h = date('G');
$m = date('i');
$d = date('w');
$hhmm = date('H') * 100 + date("i");
$img = key(array_filter(array(
'images/hosts/image1.jpg' => ($d == 0 && $h >= 0 && $h < 1),
'images/hosts/image2.jpg' => ($d == 0 && $hhmm >= 0100 && $hhmm <= 0542 ),
'images/hosts/image3.jpg' => ($d == 0 && $hhmm >= 0542 && $hhmm <= 0600),
'images/hosts/image4' => ($d == 0 && $hhmm >= 0600 && $hhmm < 0630),
'images/hosts/image5' => ($d == 0 && $hhmm >= 0630 && $hhmm < 0800),
'images/hosts/image5.jpg' => ($d == 6 && $h >= 8 && $h < 9)
If you want to compare "partial hours", then it might be best to combine hours and minutes into one value. You can often get away with hours*100+minutes:
$h = date('G');
$m = date('i');
$d = date('w');
$hhmm = data("H") * 100 + date("i");
So $hhmm would be 1230 for 12:30am, or 1942 for 7:42pm. The numbers jump between 1759 and 1800, but that's not an issue since you only want to compare with <= and => within time ranges anyway.
Also I would totally rewrite the if/else chain into an array comparison:
$img = key(array_filter(array(
'opendoor.jpg' => ($d == 1 && $h >= 0 && $h < 1),
'newtime.jpg' => ($d == 1 && $h >= 13 && $h < 14),
...
'partialhours.jpg' => ($hhmm => 2142 && $hhmm <= 2242),
)));
please use switch
switch ($d)
{
case 1:
switch ($h)
{
case 1: $img = 'opendoor.jpg'; break;
case 13: $img = 'newtime.jpg'; break;
case 14: $img = 'weekend.jpg'; break;
case 15: $img = 'today.jpg'; break;
case 16:
if ($m<30) $img = 'walk.jpg';
break;
case 17:
$img = ($m>30) ? 'new.jpg':'walk.jpg';
break;
case 18:
if ($m>30) $img = 'new.jpg';
break;
case 19:
$img = ($m>30) ? 'test.jpg':'default.jpg';
break;
}
break;
}