I have a php code as shown below in which I want to display anything in between two calendar days of the week.
The values coming inside $data->{\"select_s
You should thoroughly go through the PHP Manual and see how to create DateTime Objects and various date formats available.
First of all you need to convert the User Input to DateTimeObjects. Since the user input contains only Day (represented by D) and Time (represented by His), you could use following code to get these Objects.
date_default_timezone_set('America/Toronto'); //Setting the Timezone
//These are the User Inputs
$start_day = 'tue';
$start_time = 181300;
$end_day = 'fri';
$end_time = 134500;
//User Input Ends
//Create the Date Time Object for Start and End Date
$startDate = DateTime::createFromFormat ( 'D His', $start_day . ' '. $start_time);
$endDate = DateTime::createFromFormat ( 'D His', $end_day . ' '. $end_time);
You can check various options available for createFromFormat in the Manual
Now that you have the Date Time Objects you could get any value from them. Lets get the Day of the Week in Numeric Format as well as Hour in Numeric Format for both Start and End Date.
$startHour = $startDate->format('G'); //24-hour format of an hour without leading zeros
$startDay = $startDate->format('N'); //numeric representation of the day of the week
$endHour = $endDate->format('G'); //24-hour format of an hour without leading zeros
$endDay = $endDate->format('N'); //numeric representation of the day of the week
You can check about G and N format as well as other options available in Manual
We Need to check the time is between Tuesday 6PM to Friday 6PM.
First of all we will check whether the Day is between Tuesday and Friday. We have the $startDay and $endDay variables above to determine that.
For Tuesday, the corresponding numeric value is 2 and for Friday it is 5.
For Time Range, we have the $startHour and $endHour variables. 6PM in the format is represented as 18 ( 12 + 6).
So we can check this condition using the following code:
if ( $startDay >= 2 && $endDay <= 5 && $startHour >= 18 and $endHour <= 18 ) {
echo 'In Range';
} else {
echo 'out of Range';
}
This way you can check any complex condition. You can see the Online Demo at 3v4l