开发者

divide two dates into periods

开发者 https://www.devze.com 2023-01-25 10:35 出处:网络
lets say that a user want to take vacation from 17-10-2010 to 15-11-2010. on some setup page i declared start date andend date for each month as following:

lets say that a user want to take vacation from 17-10-2010 to 15-11-2010.

on some setup page i declared start date and end date for each month as following:

m1 :from 1-10-2010 to 20-10-2010
m2: from 21-10-2010 to 10-11-2010
m3: from 11-11-2010 tom 30-11-2010

as we can see,the vacation related to many periods(m1,m2,m3) not to only one period, so i need to divide the vacation into periods, and The array of periods should look like this:

from 17-10-2010 to 20-10-2010  
from 21-10-2010 to 10-11-2010  
from 11-11-2010 to 15-11-2010  

i think that solving the 开发者_如何学Cproblem with only using PHP will be complex,so i want to use PHP and MySQL functions to solve the problem,

Any help please?

Thanks


SELECT  GREATEST(period_start, @vacation_start),
        LEAST(period_end, @vacation_end)
FROM    mytable
WHERE   period_start <= @vacation_end
        AND period_end >= @vacation_start


I would say that first you need to see which period the first date is in. For the case being, the vacation starts in the first period (17-10-2010 >= 1-10-2010 && 17-10-2010 <= 20-10-2010), then you need to figure out in what period the end date fits. (15-11-2010 >= 11-11-2010 && 15-11-2010 <= 30-11-2010) All periods in between are covered by the vacations. What I would do in order to solve this is create a function that decides whether a date is in the period (receiving $start, $end) and then loop through all your predefined periods, the one having the vacation start being the first, the one having the vacation end being the last, and everyone between being used.

0

精彩评论

暂无评论...
验证码 换一张
取 消