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?
You can try this:
Convert both dates to timestamps then do
pseudocode:
Use the DateTime class if you have PHP 5.3+. Easier to use, better functionality.
DateTime internally supports timezones, with the other solutions is up to you to handle that.
Converting them to timestamps is the way to go alright, using strtotime, e.g.
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:
given:
NOTE: Comparing strings like this does allow for "non-valid" dates e.g. (December 32nd ) '2009-13-32' and for weirdly formatted strings '2009/3/3', such that a string comparison will NOT be equivalent to a date or timestamp comparison. This works ONLY if the date values in the strings are in a CONSISTENT and CANONICAL format.
EDIT to add a note here, elaborating on the obvious.
By CONSISTENT, I mean for example that the strings being compared must be in identical format: the month must always be two characters, the day must always be two characters, and the separator character must always be a dash. We can't reliably compare "strings" that aren't four character year, two character month, two character day. If we had a mix of one character and two character months in the strings, for example, we'd get unexpected result when we compared,
'2009-9-30'
to'2009-10-11'
. We humanly see "9" as being less than "10", but a string comparison will see'2009-9'
as greater than'2009-1'
. We don't necessarily need to have a dash separator characters; we could just as reliably compare strings in'YYYYMMDD'
format; if there is a separator character, it has to always be there and always be the same.By CANONICAL, I mean that a format that will result in strings that will be sorted in date order. That is, the string will have a representation of "year" first, then "month", then "day". We can't reliably compare strings in
'MM-DD-YYYY'
format, because that's not canonical. A string comparison would compare theMM
(month) before it comparedYYYY
(year) since the string comparison works from left to right.) A big benefit of the 'YYYY-MM-DD' string format is that it is canonical; dates represented in this format can reliably be compared as strings.[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