PHP Check if time is between two times regardless of date
Try this function:
function isBetween($from, $till, $input) {
$f = DateTime::createFromFormat('!H:i', $from);
$t = DateTime::createFromFormat('!H:i', $till);
$i = DateTime::createFromFormat('!H:i', $input);
if ($f > $t) $t->modify('+1 day');
return ($f <= $i && $i <= $t) || ($f <= $i->modify('+1 day') && $i <= $t);
}
demo
How to check if time is between two times in PHP
$current_time = "4:59 pm";
$sunrise = "5:42 am";
$sunset = "6:26 pm";
$date1 = DateTime::createFromFormat('h:i a', $current_time);
$date2 = DateTime::createFromFormat('h:i a', $sunrise);
$date3 = DateTime::createFromFormat('h:i a', $sunset);
if ($date1 > $date2 && $date1 < $date3)
{
echo 'here';
}
See it in action
Reference
- DateTime
how to check if two datetime between current times
If dates are part of your logic, then use them. You can utilize DateTime
's relative formats:
$start = new DateTime('today 08:00 AM');
$end = new DateTime('tomorrow 02:00 AM');
$current = new DateTime();
if ($start <= $current && $current <= $end) {
// do something
}
PHP check if pass 5 minutes between two dates
Sorry, I don't mean to be rude but I was confused by the question. What I designed here was to see if time B is five minutes after Time A.
A few notes:
No need to bother with strtotime or date. Just keep everything in unix time. Compare the seconds by using 60 * 5. 60 for 60 seconds in a minute and 5 for the number of minutes.
<?php
//Time A
$timeA = '1518223062';
//Time B (Which I set at the current time)
$timeB = time();
//five minutes in seconds
$fiveMinutes = 60 * 5;
//check if current time is after 5 minutes the initial time
if ( ($timeA+$fiveMinutes) <= $timeB) {
echo "True";
}
else {
echo "False";
}
?>
Check if a given time lies between two times regardless of date
You can use the Calendar
class in order to check.
For example:
try {
String string1 = "20:11:13";
Date time1 = new SimpleDateFormat("HH:mm:ss").parse(string1);
Calendar calendar1 = Calendar.getInstance();
calendar1.setTime(time1);
calendar1.add(Calendar.DATE, 1);
String string2 = "14:49:00";
Date time2 = new SimpleDateFormat("HH:mm:ss").parse(string2);
Calendar calendar2 = Calendar.getInstance();
calendar2.setTime(time2);
calendar2.add(Calendar.DATE, 1);
String someRandomTime = "01:00:00";
Date d = new SimpleDateFormat("HH:mm:ss").parse(someRandomTime);
Calendar calendar3 = Calendar.getInstance();
calendar3.setTime(d);
calendar3.add(Calendar.DATE, 1);
Date x = calendar3.getTime();
if (x.after(calendar1.getTime()) && x.before(calendar2.getTime())) {
//checkes whether the current time is between 14:49:00 and 20:11:13.
System.out.println(true);
}
} catch (ParseException e) {
e.printStackTrace();
}
Related Topics
How to Add a Watermark to an Existing PDF File Using PHP
Remove Zero Values from a PHP Array
Dirt-Simple PHP Templates... Can This Work Without 'Eval'
Session_Start() Takes Very Long Time
Utf-8 Problems While Reading CSV File with Fgetcsv
Difference Between Require and Install VS Create-Project in Composer
Google API How to Connect to Receive Values from Spreadsheet
How Does Pcntl_Fork Work in PHP
How to Add Commas to Numbers in PHP
Safe Alternatives to PHP Globals (Good Coding Practices)
If You Create a Variable Inside a If Statement Is It Available Outside the If Statement
Kill MySQL Query on User Abort