简体   繁体   中英

How to find if a date fits an interval in either PHP or MySQL?

Let's say I have a datetime, June 16 2011 at 7:00. I want to be able to check at, say, August 5 2011 at 7:00 and be able to tell that it is exactly a multiple of 1 day since the first date, whereas 7:01 would not count, since it is not an exact multiple.

Another test set: Let's say we have June 16 2011 at 7:00, and I want to check if a particular minute is within an interval of exactly 2 hours since then. So 9:00, 11:00, 13:00, etc. would count, but 9:30 and 10:00 would not. And this could continue for days and months - September 1 at 7:00 would still count as within every 2 hours. (And no, at the moment I don't know how I'm going to handle DST:D)

I thought about it for a moment and couldn't think of anything already existing in PHP or MySQL to do this easily but hell, it could, so I wanted to throw this up and ask before I start reinventing the wheel.

This is on PHP 5.1, sadly.

Easiest would be to convert the date/time values into a unix timestamp and then simply do some subtraction/division:

2011-06-16 07:00:00 -> 1308229200
2011-08-05 07:00:00 -> 1312549200
2011-08-05 07:00:01 -> 1312549201

1312549200 - 1308229200 = 4320000 / 86400 = 50 (days)
1312549201 - 1308229200 = 4320001 / 86400 = 50.0000115...

So in other words:

if (($end_timestamp - $start_timestamp) % 864000)) == 0) {
  ... even multiple ...
}

Same would hold for the day/week comparisons. For months, this'll be out the window, since months aren't nice even figures to deal with.

select *
from test
where datetimefield > '2011-06-16 07:00:00'
and
mod(timestampdiff(second,'2011-06-16 07:00:00',datetimefield),7200) = 0

This example will give you all the records greater than '2011-06-16 07:00:00' where the field is exactly a multiple of 2 hours.

MySQL Date functions: http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html

  1. You can use TIME() to get just the time part of a date. If the time parts are the same it is an exact multiple.

  2. For the two hour thing, one way to do it would be to get the minute/seconds part of the date, make sure those are equal, then make sure that the hour parts of the dates are both even or both odd. For more complicated integer (eg 5) hour multiples, you can "fake" doing a mod by dividing the hour parts and checking if the result is an int.

Check this thread's top answer for info on PHP functions to get difference in days between 2 dates and manipulate said results: Calculate number of hours between 2 dates in PHP

Then you could mod the number of hours by your required number of hours, and if the result is zero then it's a match.

You can compare two DateTime objects via diff() method. Result is a DateInterval object - you can check the exact number of days/hours/minutes between two dates.

It's useless to write your own algorithms if you can use built-in functionality.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM