Check between time in a period

Asked

Viewed 29 times

-1

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

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 inform the beginning of 10:00 until 14:40 and the server is within this period, the function returns me true.

The problem is when I use a period that turns the day, that is, the end time is less because it refers to the other day. Example: 18:00 until 8:00.

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

  • Could you perform day and time check? or does it need to be ONLY the time?

  • Yes, it has to be just the hour. I want to program the system to identify a company’s business hours and notify me of any access made to the system without being in the operating hours.

1 answer

0


You can use spy classes to work with date and time, such as date time, because she has support for this matter of days difference. You can still, too, work with a more complete class that extends the date time, which is the Carbon, all of them have the support for what 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 the time as a string, and you don’t have that control.

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

<?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 the Carbon you have some methods that are the

*eq() equals

*ne() not equals

*gt() greater than

*gte() greater than or equals

*lt() less than

*lte() less than or equals

Example use of Carbon with a timestamps column in the database

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.