开发者

How to create an interval of times in PHP based on a start time?

开发者 https://www.devze.com 2023-04-10 04:58 出处:网络
I\'m wondering how given a start time, I can calculate a series of time spans in intervals and execute code during that time.

I'm wondering how given a start time, I can calculate a series of time spans in intervals and execute code during that time.

For example: I start at 3:30pm and have 4, 45 minute intervals that need to be calculated based on the start time. Then between 3:00pm and 3:45pm I want to be able to check if the current time is between that span and then execute a block of code if it is.

The gist: A company wants to give out 20 gift cards dur开发者_StackOverflow社区ing a 3 hour span. They want to give out only 5 every 45 minutes.

Any thoughts would be appreciated.


As Walkerneo said, the best way to do this is to use a cron job on the server and maybe store the data for the gift cards in a MySQL that will be checked when the cron runs the php with the funcions...

The other (very bad way) to do this is keep a loop inside your php file to check if it's time to give out the gift cards, but this is a VERY VERY BAD idea because of the high CPU usage in the server...

If you want to give the the gift cards when the user opens a page or buys something, it's easy, just setup a table in MySQL with the time spans where the user can get the gift cards and then when we submits the form to buy or check it's PHP should go the table and check if you're in any of the time spans.

Hope it helps...

0

精彩评论

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

关注公众号