I want to check if a given date/time lies within a certain range of dates/times.
I'm currently using the Date Pcalc module to check if a specific date is within the range.
But I'm having issues while building the logic to compare the times.
The date comparison is fine. See the last if statement; this is where I'm having some trouble. You'll see the logic is wrong.
I'm currently using the Date Pcalc module to check if a specific date is within the range.
But I'm having issues while building the logic to compare the times.
The date comparison is fine. See the last if statement; this is where I'm having some trouble. You'll see the logic is wrong.
Code:
# Date 1 & 2
$Date1 = "2007-06-07";
$Date2 = "2007-06-08";
# Time 1 & 2 (24-Hour Clock Format)
$Time1 = "8:30";
$Time2 = "10:30";
# Current Date
$CurrentDate = "2007-06-08";
# Current Time
$CurrentTime: "9:16";
# Convert Date Strings to Arrays
my @ArrayDate1 = split(/\-/,$Date1);
my @ArrayDate2 = split(/\-/,$Date2);
my @ArrayDateCurrent = split(/\-/,$CurrentDate);
# Convert Dates to Days
my $Days_to_Date1 = Date_to_Days(@ArrayDate1);
my $Days_to_Date2 = Date_to_Days(@ArrayDate2);
my $Days_to_DateCurrent = Date_to_Days(@ArrayDateCurrent);
# Check if Current Date Lies between Date 1 and Date 2
if (($Days_to_DateCurrent >= $Days_to_Date1) && ($Days_to_DateCurrent <= $Days_to_Date2)) {
# PROBLEM IS HERE; Logic is wrong
# If Date1 or Date2 is TODAY; check Time
if (($Days_to_DateCurrent == $Days_to_Date1 && $CurrentTime ge $Time1) || ($Days_to_DateCurrent == $Days_to_Date2 && $CurrentTime le $Time2)) {
print "Yes";
}
}