Reputation: 9726
My event has start time and duration (for example, start time is 22:00
, duration is 3 hours). I can calculate event end time, for current example it would be 01:00
.
So, finally i have:
22:00
01:00
x
(i should find out if it belongs to specified interval), for example 2013-11-05 23:00:00
(i have date part only here)I need to detect if event will occur in specific time (if point x
belongs to this event time interval). Event could have duration > 24h. I do not know any dates about events (these are recurrent events)
How is it possible to do in php?
Upvotes: 0
Views: 451
Reputation: 3299
If the start time is smaller than or equals to the time of x
, assume the start date is the date of x
. Otherwise assume the start date is the day before the date of x
.
For the example the former is the case, so the start date and time will be 2013-11-05 22:00:00
. With this, check match.
If the start time would be e.g. 23:30
, than the start date would be the previous day, 2013-11-04 23:30:00
. This is required, because if the time of x
is e.g. 01:00
, you won't get a match if the start day is the current day.
Upvotes: 1
Reputation: 32232
The function you're looking for is strtotime()
and it's worth noting that it's not magic. You must be certain that the date strings you feed it conform to the Supported Date and Time Formats.
$start = strtotime("2013-11-05 22:00:00");
$duration = 3 * 3600;
$end = $start + duration;
$x = strtotime("2013-11-05 23:00:00");
if( ($x <= $end) && ($x >= $start) ) {
echo "x is in time period.";
} else { echo "nope."; }
Also, the timestamp is returned in UTC, calculated based on the timezone set in PHP which defaults to that which is set on your server. Be aware of this.
As seen in @mcuadros's answer, you can also feed strtotime()
relative time specifications like today 22:00 +3 hours
which works out to the same as tomorrow 01:00
.
Upvotes: 1