How to check if a date is in a given range?

If you have $start_date and $end_date , how can you check if a user-defined date falls into this range?

eg.

 $start_date = '2009-06-17'; $end_date = '2009-09-05'; $date_from_user = '2009-08-28'; 

Dates are currently strings, would they help convert them to integers in time?

+62
date php date-range
Jun 10 '09 at 16:18
source share
10 answers

Converting them to a timestamp is a way to go well using strtotime , e.g.

 $start_date = '2009-06-17'; $end_date = '2009-09-05'; $date_from_user = '2009-08-28'; check_in_range($start_date, $end_date, $date_from_user); function check_in_range($start_date, $end_date, $date_from_user) { // Convert to timestamp $start_ts = strtotime($start_date); $end_ts = strtotime($end_date); $user_ts = strtotime($date_from_user); // Check that user date is between start & end return (($user_ts >= $start_ts) && ($user_ts <= $end_ts)); } 
+92
Jun 10 '09 at 16:24
source share

No need to convert to timestamp for comparison, given that the strings are checked as dates in the canonical format "YYYY-MM-DD".

This test will work:

 ( ( $date_from_user >= $start_date ) && ( $date_from_user <= $end_date ) ) 

Given:

 $start_date = '2009-06-17'; $end_date = '2009-09-05'; $date_from_user = '2009-08-28'; 

NOTE. Comparing strings like this allows for "invalid" dates, for example. (December 32) "2009-13-32" and for strangely formatted strings "2009/3/3", so string comparisons will NOT be equivalent to date or time comparisons. This works ONLY if the date values ​​in the strings are in the CONSISTENT and CANONICAL format .

EDIT to add a note here stating the obvious.

By CONSISTENT, I mean, for example, that the lines to be compared must be in the same format: the month should always be two characters, the day should always be two characters, and the separator character should always be dashes. We cannot reliably compare “strings” that are not a four-year character, a two-character month, two characters. If we had a combination of one character and two-digit months in lines, for example, we would get an unexpected result when comparing, '2009-9-30' - '2009-10-11' . We humanly see that "9" is less than "10", but string comparisons will be '2009-9' more than '2009-1' . We do not have to have dash separator characters; we could reliably compare strings in the format 'YYYYMMDD' ; if there is a separation character, it must always be there and always be the same.

By CANONICAL , I mean a format that will result in strings that will be sorted in date order. That is, the line will have the representation of “year” first, then “month”, then “day”. We cannot reliably compare strings in the 'MM-DD-YYYY' format because this is not canonical. String comparison would compare MM (month) before it compared YYYY (year), since string comparison works from left to right.) The big advantage of the "YYYY-MM-DD" string format is that it is canonical; dates presented in this format can be reliably compared as strings.

[ADDITION]

If you want to convert php timestamp, keep in mind the limitations.

On some platforms, php does not support timestamp values ​​earlier than 1970-01-01 and / or later than 2038-01-19. (What is the character unix timestamp 32-bit integer.) Later versions of pf php (5.3?) Should address this.

The time zone can also be a problem if you are not careful to use the same time zone when converting from a string to a time stamp and from a time stamp to a string.

NTN

+45
Jun 10 '09 at 16:25
source share

Use the DateTime class if you have PHP 5.3+. Ease of use, improved functionality.

DateTime internally supports time zones, and other solutions will help you deal with this.

 <?php /** * @param DateTime $date Date that is to be checked if it falls between $startDate and $endDate * @param DateTime $startDate Date should be after this date to return true * @param DateTime $endDate Date should be before this date to return true * return bool */ function isDateBetweenDates(DateTime $date, DateTime $startDate, DateTime $endDate) { return $date > $startDate && $date < $endDate; } $fromUser = new DateTime("2012-03-01"); $startDate = new DateTime("2012-02-01 00:00:00"); $endDate = new DateTime("2012-04-30 23:59:59"); echo isDateBetweenDates($fromUser, $startDate, $endDate); 
+36
Jan 30 2018-12-12T00:
source share
 $startDatedt = strtotime($start_date) $endDatedt = strtotime($end_date) $usrDatedt = strtotime($date_from_user) if( $usrDatedt >= $startDatedt && $usrDatedt <= $endDatedt) { //..falls within range } 
+4
Jun 10 '09 at 16:24
source share

Convert both dates to timestamps, then do

pseudo code:

 if date_from_user > start_date && date_from_user < end_date return true 
+3
Jun 10 '09 at 16:23
source share

In the format that you provided, assuming the user is smart enough to give you valid dates, you do not need to convert to a date first, you can compare them as strings.

+3
Jun 10 '09 at 16:25
source share
 $start_date="17/02/2012"; $end_date="21/02/2012"; $date_from_user="19/02/2012"; function geraTimestamp($data) { $partes = explode('/', $data); return mktime(0, 0, 0, $partes[1], $partes[0], $partes[2]); } $startDatedt = geraTimestamp($start_date); $endDatedt = geraTimestamp($end_date); $usrDatedt = geraTimestamp($date_from_user); if (($usrDatedt >= $startDatedt) && ($usrDatedt <= $endDatedt)) { echo "Dentro"; } else { echo "Fora"; } 
+3
Dec 18 '11 at 6:00
source share

Convert them to dates or integers of time, and then just check the value of $ date_from_user is <= $ end_date and> = $ start_date

+2
Jun 10 '09 at 16:23
source share

You can try the following:

 //custom date for example $d1 = new DateTime("2012-07-08"); $d2 = new DateTime("2012-07-11"); $d3 = new DateTime("2012-07-08"); $d4 = new DateTime("2012-07-15"); //create a date period object $interval = new DateInterval('P1D'); $daterange = iterator_to_array(new DatePeriod($d1, $interval, $d2)); $daterange1 = iterator_to_array(new DatePeriod($d3, $interval, $d4)); array_map(function($v) use ($daterange1) { if(in_array($v, $daterange1)) print "Bingo!";}, $daterange); 
+1
Mar 25 '14 at 15:34
source share

I found this method the easiest:

 $start_date = '2009-06-17'; $end_date = '2009-09-05'; $date_from_user = '2009-08-28'; $start_date = date_create($start_date); $date_from_user = date_create($date_from_user); $end_date = date_create($end_date); $interval1 = date_diff($start_date, $date_from_user); $interval2 = date_diff($end_date, $date_from_user); if($interval1->invert == 0){ if($interval2->invert == 1){ // if it lies between start date and end date execute this code } } 
0
Jun 20 '16 at 11:05
source share



All Articles