views:

1859

answers:

6

If you have a $start_date and $end_date, how can you check if a date given by the user falls within that range?

e.g.

$start_date = '2009-06-17';

$end_date = '2009-09-05';

$date_from_user = '2009-08-28';

At the moment the dates are strings, would it help to convert them to timestamp integers?

A: 

Convert them into dates or timestamp integers and then just check of $date_from_user is <= $end_date and >= $start_date

TheTXI
Could you clarify 'convert them into dates'? How would you do that? I thought PHP has no date type?
meleyal
A: 

Convert both dates to timestamps then do

pseudocode:

if date_from_user > start_date && date_from_user < end_date
    return true
Glen
You might want to edit it so that you also include start_date and end_date in the range. Right now if date_from_user equals either, it will not be counted.
TheTXI
The OP didn't specify inclusive or exclusive ranges, so I'll leave it up to them to pick which strategy to use
Glen
A: 
$startDatedt = strtotime($start_date)
$endDatedt = strtotime($end_date)
$usrDatedt = strtotime($date_from_user)

if( $usrDatedt >= $startDatedt && $usrDatedt <= $endDatedt)
{
   //..falls within range
}
Stan R.
That will not include any exact matches to the start date or end date
TheTXI
thx. typo. i fixed code.
Stan R.
OP didn't ask for inclusive matches.
fiXedd
+4  A: 

Converting them to timestamps is the way to go alright, 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));
}
ConroyP
+7  A: 

No, it's not necessary to convert to timestamp to do the comparison, given that the strings are validated as dates in 'YYYY-MM-DD' canonical format.

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 allows for "non-valid" dates e.g. '2009-13-32' and for weirdly formatted strings '2009/3/3', such that the string comparison is not equivalent to a date or timestamp comparison.

[ADDENDUM]

If you do go for the php timestamp conversion, be aware of the limitations.

On some platforms, php does not support timestamp values earlier than 1970-01-01 and/or later than 2038-01-19. (That's the nature of the unix timestamp 32-bit integer.) Later versions pf php (5.3?) are supposed to address that.

The timezone can also be an issue, if you aren't careful to use the same timezone when converting from string to timestamp and from timestamp back to string.

HTH

spencer7593
This one is interesting.
Ionuț G. Stan
Although without casting you could run into problems if the values of the $date_from_user, or $start_date, or $end_date are not dates.. i.e. $end_date = 'Balh Blah'.. will definitely not work correctly
Miky Dinescu
@spencer7593, do you have some references for this behavior?
Ionuț G. Stan
You could use checkdate() to validate
meleyal
+2  A: 

In the format you've provided, assuming the user is smart enough to give you valid dates, you don't need to convert to a date first, you can compare them as strings.

richardtallent
By this point in the code the dates have already been validated
meleyal