S
S
Sergey2020-12-03 12:22:24
PHP
Sergey, 2020-12-03 12:22:24

How to understand whether the date is in the interval or not?

Good afternoon.

Tell me how to make a correct comparison of dates, check whether the current date is included in the time interval or not.

Thank you.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
N
nokimaro, 2020-12-03
@nokimaro

<?php
$checkDate1 = new DateTime("2020-06-25");
$checkDate2 = new DateTime("2020-01-30");
$startDate = new DateTime("2020-06-20 00:00:00");
$endDate = new DateTime("2020-06-30 23:59:59");

//bool(true)
var_dump(
    isDateBetweenDates($checkDate1, $startDate, $endDate)
);

//bool(false)
var_dump(
    isDateBetweenDates($checkDate2, $startDate, $endDate)
);

function isDateBetweenDates(DateTime $date, DateTime $startDate, DateTime $endDate) {
    return $date > $startDate && $date < $endDate;
}

F
FanatPHP, 2020-12-03
@FanatPHP

If the date is in normal format, then just like any other value.
You can practice on cats first.
3 is in the range from 2 to 5? and 7?
after you write the code for these checks, it will be the same for dates.

A
Alexander, 2020-12-03
@AleksandrB

$date = '17.03.2020';
$timestamp = strtotime($date);

$from = strtotime('19.03.2020');
$to = strtotime('23.05.2020');

if ($timestamp > $from && $timestamp < $to) {
    return true;
} else {
    return false;
}

I
index0h, 2020-12-04
@index0h

Tell me how to make a correct comparison of dates, check if the current date is included in the time interval or not.

1. Convert dates to numeric form (UNIX timestamp)
2. Compare with start timestamp and end timestamp

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question