Converting a user submitted date to GMT |
Code igniter 2.x
I need to convert a form filled date and time and time zone to GMT User submits a date field like 7/2/2015 Time field like 8:00 PM and a timezone like: UM5 I found this page: https://ellislab.com/codeigniter/user-gu...elper.html But I am still not sure how to go about this. Thank you for your time. Randy
Honestly, I've always had trouble trying to figure out the intended method of working with the CI timezones, because there seem to be too many pieces missing to make it possible to really convert between user, application, and server timezones. For this reason, I created a very crude function (which can be placed in a MY_date_helper.php file to be loaded whenever the CI date_helper is loaded) to get a PHP timezone string from a CI timezone string. Then you can use PHP's built-in DateTime methods to convert the date/time to GMT.
Here is what I came up with.
What do you think? PHP Code: function convert_to_utc($date, $time, $tz){ PHP Code: $da = $this->convert_to_utc("07/02/15","9:06 PM","UM5"); PHP Code: Array
You seem to have avoided most of the common pitfalls with PHP DateTimes, so I would say it looks good.
One thing to be wary of is the format of the date string being supplied. strtotime behaves differently depending on whether '-' or '/' is used in the date string, and can potentially cause issues getting the accurate time. I ran into this since I've done a lot of work for a client out of Manchester, UK, but I live in USA. From the PHP manual:
Quote:Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. strtotime does support parsing out the timezone as part of the string, so it might work to ensure the timezone string is part of the string passed to the strtotime() function, but would need testing. For the project that I had problems on, we knew that all users would be UK area so I didn't have to explore quite that deep and ended up using DateTime::createFromFormat() where possible. |
Welcome Guest, Not a member yet? Register Sign In |