Checking time in a period

-1

Well I'm trying to create a function that returns me true / false based on a certain time period. It buys the server's current time and checks to see if it is within the specified time.

Example:

function Verifica_Expediente ($inicio, $fim) {

    // Coleta dados
    $hora1 = strtotime(date('Y-m-d' . $inicio));
    $hora2 = strtotime(date('Y-m-d' . $fim));
    $agora = time();

    // Verifica horas
    if ($hora1 <= $agora && $agora <= $hora2) {
        return true;
    } else {
        return false;
    }
}

Verifica_Expediente ("10:00:00", "14:30:00");

When I report the start from 10:00 AM until 2:40 PM and the server is within that period, the function returns me true .

The problem is when I use a period that turns the day, ie the end time is less because it is referring to the other day. Example: 6:00 p.m. to 8:00 p.m.

Does anyone know how I can verify this? I need to identify that the day has changed.

    
asked by anonymous 19.09.2018 / 13:30

1 answer

0

You can use specific classes to work with date and time, such as date time , because it has support for this matter of difference of days. You can also work with a more complete class that extends the date time, which is the carbon , all of which you have support for this you need, because internally in the class, they have the control of day, month and year. In your case, as I understand it, you only have time as a string, and you do not have that control.

Example of what you need to do with the class date time:

<?php
date_default_timezone_set('Europe/London');

$d1 = new DateTime('2008-08-03 14:52:10');
$d2 = new DateTime('2008-01-03 11:11:10');
var_dump($d1 == $d2); #bool(false)
var_dump($d1 > $d2); #bool(true)
var_dump($d1 < $d2); #bool(false)
?>

And with Carbon you have some methods that are

*eq() equals

*ne() not equals

*gt() greater than

*gte() greater than or equals

*lt() less than

*lte() less than or equals

Example of using carbon with a timestamps column in the database

if($model->edited_at->gt($model->created_at)){
    // edited at is newer than created at
}
    
19.09.2018 / 13:41