I am building an App that fetches all entries a user has made “this week” (in their timezone). I store all entry times in the database as UTC/GMT time using:
gmdate('U')
My problem arises when I need to fetch “this week” in their timezone. Before i took timezones into account i used…
$startWeek = strtotime('monday this week 00:00')
$endWeek = strtotime('sunday this week 00:00')
…and used a SQL statement that searched BETWEEN those two variables.
The problem is, those two variables are using the server times, so it gives the servers start and end of the week.
I have access to the users timezone (ie: Australia/Melbourne) in a cookie if needed.
Bottom line, how do I fetch the beginning and end of the week, in timestamp format, in the timezone i have stored in a cookie?
I think
date_default_timezone_setfunction fits your requirement:http://www.php.net/manual/en/function.date-default-timezone-set.php
You need to get the timezone from the user and then call the function with it. Going from the timezone, the following snippet should work: