Don’t be confused, think about what the logic needs to be.
If “time” - however that is expressed, eg. string, int, etc. is “something” then “do this”.
The “something to something” suggests you want a “between”.
In code, “between” is typically written as a “greater than this and less than that”.
I find that once I can write something in non-code English so it makes sense and looks OK I can write the code for it more easily. Give that a try (writing the logic in non-code) and see how you do.
Working with dates / times can be confusing. What looks “best” for humans is infrequently what’s best for code.
For example, if I see something like a timestamp of “6783984608” it means next to nothing to me. I prefer either American or computer style like “Mar 17, 2018” or “2018-03-18”
I find it works best to accept input in “human” format, change it to “code” format" and use that until the code processing is done, and then changing it back to “human” format to display it.
AFAIK, timestamp format (data type integer) is the most efficient, but computer style (data type string) works well too as long as documentation is checked to make sure the code doesn’t contain “gotchas”.(for example, if a month with 30 days is compared to a month with 31 days then something you might not expect will happen)
I use this php too but it not working fir redirect time based…
Why? Where is the issue~
<?php
/**
* Get URL to redirect to based on time of day
* call with no arguments to use current time, or
* pass in 24 hour number representing hour to test for different times
* Requires >= PHP7.0. For older versions change the '$hour = $currentHour ??...' line below
*/
function timeOfDayRedirection($currentHour = null) {
$hour = ($currentHour) ? $currentHour : (int)date('G');
//9am - 8:59:59pm
if($hour >= 9 AND $hour <= 20) {
return 'https://start-website.com';
}
//3am - 8:59:59am
if($hour >= 21 AND $hour <= 8) {
return 'https://sleep-website.com';
}
redirect( timeOfDayRedirection() );
exit;
The main issue is I am not getting proper your language. Yes you write in english but me and your english is too different. That why I don’t try which you saying…
I’m sure language is part of the problem, but the main problem is that you are trying to do some very complicated things when you don’t have a good grasp of the basics. You need to learn to walk before you try to run.
OK, another way to think of it. Look at this line of code:
if($hour >= 21 AND $hour <= 8) {
Now, forget about this being an hour, and just think about it being a number. So we’re not talking about “before” and “after”, but exactly what the code says, “greater than” and “less than”. Now think of whether it’s possible to have a number that is both larger than 21, and less than 8, at the same time.
Anyway, having the numbers split to where they correspond to where humans split the hours of a day will mean that slightly more verbose conditionals will be needed. Working with “24 hour time” instead of 12 hour AM and PM, will make it a bit easier without a “noon” split, but you will still need to deal with the “midnight” split.
For example, you can’t have a third shift as 2300 to 0700, it would need to be 2300 - 2400 and 0000 - 0700