问题
I need to send reminder sms everyday to all users who must check their Blood Pressure in given time range. Records in my schedule table are:
user_id |name |checkup_time |timezone
1 | user1 | 01:30:00 | PST
2 | user2 | 03:00:00 | CST
Currently I am able to get list of users whose checkup time is between current_time and (current time + 30 mins).
I have used following mysql query to achieve this:
$checkup_time_min = date('H:i:s');
$checkup_time_max = date("H:i:s", strtotime($currTime ."+30 minutes"));
SELECT ... WHERE checkup_time BETWEEN '".$checkup_time_min."' AND '".$checkup_time_max."'
However, I want the time_zone to be considered in this scenario.
I would really appreciate any help or hints.
Thanks in advance
回答1:
You probably would want to store the times as UTC in the database, this would make queries like this much more logical and easy to understand. When you pull the times from the DB you can also pull the timeozne and make any conversion necessary at that time.
As it is, you could query for all values between
$checkup_time_min +- (time conversion for given timezone) and
$checkup_time_max -+ (time conversion for given timezone)
but you would need to run a different query for each timezone. Alternatively you could do a bunch of if statements checking the timezone but that isn't much cleaner.
回答2:
Thanks for giving directions. I've found that CONVERT_TZ is what I needed.
Taking suggestion from Matt Johnson into consideration, I've changed the data in column timezone as follows: (from CST to -05:00)
user_id |name |checkup_time |timezone
1 | user1 | 01:30:00 | -08:00
2 | user2 | 03:00:00 | -05:00
And the code is changed to
$checkup_time_min = date('Y-m-d H:i:s');
$checkup_time_max = date("Y-m-d H:i:s", strtotime($currTime ."+30 minutes"));
SELECT ... WHERE checkup_time BETWEEN CONVERT_TZ ('".$checkup_time_min ."','-05:00',timezone) AND CONVERT_TZ ('".$checkup_time_max ."','-05:00',timezone)
Note: Server's timezone is CST thus the conversion is done to '-05:00'
来源:https://stackoverflow.com/questions/29474861/check-if-timewith-timezone-lies-in-given-time-range