Php check time between two times

i would recoment you to first "convert" the strings to integers using substr function and intval.

avter this step you should be able to simply compare the times.

( note that you should use 18:00 instead of 6:00 PM, this would be easyer to handl, therefor you could "detect" with substr if the time is AM or PM, and just add "12" to the number )

i'm pretty shure there are more elegant ways of doing this, but this should just give you an BASIC idea for a SIMPLE way for not advanced php developers.

hope it helps

By default there's no method available on the DateTime or DateInterval class to get the difference between two DateTime objects in seconds. Therefore, you can either get the difference of the timestamps of the two dates or calculate the number of seconds yourself:

Comparing Timestamps to Get Number of Seconds Between Two Dates

We can simply compare timestamps of two dates to get the difference in seconds. For example:

$start = new DateTime('2011-12-31 00:00:00');
$end = new DateTime('2021-01-01 00:00:00');

echo $end->getTimestamp() - $start->getTimestamp(); // output: 284169600

Please note that comparing timestamps could lead to problems with dates before 1970 and after 2038.

Calculating Number of Seconds Between Two Dates

You can calculate the number of seconds between the two dates in the following way:

$start = new DateTime('2011-12-31 00:00:00');
$end = new DateTime('2021-01-01 00:00:00');
$diff = $start->diff($end);

$daysInSecs = $diff->format('%r%a') * 24 * 60 * 60;
$hoursInSecs = $diff->h * 60 * 60;
$minsInSecs = $diff->i * 60;

$seconds = $daysInSecs + $hoursInSecs + $minsInSecs + $diff->s;

echo $seconds; // output: 284169600

The calculation works in the following way:

  • Using the %r formatting character, adds the minus sign when the result is negative;
  • Using the %a formatting character gives us the total number of days between two dates;
  • Using the %r%a format together, we can get the negative/positive number of days. Using that, we can convert the number of days into seconds by multiplying it with hours in a day, minutes in an hour, and seconds in a minute (i.e. days * 24 * 60 * 60);
  • The calculation in the last step does not take into account the hours, minutes and seconds in the date difference (as they're not included in the %a format). To include those, we simply use the hour, minute and second properties available on the DateInterval object and convert them into seconds as shown in the example above.


Hope you found this post useful. It was published 01 Jan, 2021. Please show your love and support by sharing this post.

PHP Code Check time difference is between two time slots

We are going to explain how to get the time difference between timeslots. below are the simple example given to get the time difference between the two given slots. As you can see we have used the inbuilt function DateTime::createFromFormat() or strtotime() of PHP to parse the time string according to the required format it returns the result.  


Below are the simple example in PHP to get the time diffrence between time slots 

<?php

function checkIfExist($fromTime, $toTime, $input) {

         $fromDateTime = DateTime::createFromFormat("!H:i", $fromTime);    
    $toDateTime = DateTime::createFromFormat('!H:i', $toTime);
    $inputDateTime= DateTime::createFromFormat('!H:i', $input);
    if ($fromDateTime> $toDateTime) $toDateTime->modify('+1 day');
    return ($fromDateTime <= $inputDateTime&& $inputDateTime<= $toDateTime) || ($fromDateTime <= $inputDateTime->modify('+1 day') && $inputDateTime<= $toDateTime);
}

$result=checkIfExist("08:00","18:00","09:00");
echo $result; // Result 1 - true

?>

The second possible way to get the time difference between time slots below are the given sample 

Getting the time difference between two time slots in PHP, You can use strtotime() for time calculation. Based on your requirement you can customize the script. 
Here is another simple example in PHP to get the time difference between time slots: 
<?php
  $checkTime = strtotime('09:00:59');
  echo 'Check Time : date('H:i:s');
  echo strtotime('2013-03-30 21:00') - strtotime('2013-03-29 21:00'); 
  echo strtotime('2013-03-31 21:00') - strtotime('2013-03-30 21:00'); 
  //Result  you get 86400 and 82800.
?>

How to check time is between two times in PHP?

$current_time = date('h:i:s a'); and if we use >= , <= in if condition then we'll get accurate answer..

How can I get minutes between two dates in PHP?

We will be using the built-in function date_diff() to get the time difference in minutes. For this, we will be needed a start date and end date to calculate their time difference in minutes using the date_diff() function. Syntax: date_diff($datetime1, $datetime2);

How to get seconds from datetime in PHP?

If you want to convert datetime to seconds use the strtotime() from PHP. The MySQL syntax is as follows: SELECT TIME_TO_SEC(ABS(timediff('yourDateTimeValue',now()))); Now you can convert PHP datetime to seconds with the help of strtotime().

How can I calculate total hours in PHP?

There are two ways to calculate the total time from the array. Using strtotime() function: The strtotime() function is used to convert string into the time format. This functions returns the time in h:m:s format. Example 1: This example reads the values from the array and converts it into the time format.