I am confused while using php to handle date/time.
What I am trying to do is this: When a user visits my page I am asking his timezone and then displaying the 'day of week' in his timezone.
I don't want to use the browser's day. I want to do this calculation in php.
This is how I am trying to achieve it:
- The timezone entered by user
- Unix time stamp calculated by php time() function.
But I dont know how to proceed... How would i get the 'day of week' in this timezone.
This should work (didn't test it, so YMMV). It works by storing the script's current timezone, changing it to the one specified by the user, getting the day of the week from the
date()
function at the specified timestamp, and then setting the script's timezone back to what it was to begin with.You might have some adventures with timezone identifiers, though.
My solution is this:
Output is:
Tuesday
Output is:
Tue
Another quick way:
Thanks a lot guys for your quick comments.
This is what i will be using now. Posting the function here so that somebody may use it.
Report if you find any corrections.
If you can get their timezone offset, you can just add it to the current timestamp and then use the gmdate function to get their local time.
Based on one of the other solutions with a flag to switch between weeks starting on Sunday or Monday