Welcome Guest, Not a member yet? Register   Sign In
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.


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.

Thank You. That was what I needed to get me on the right track.

Here is what I came up with.

What do you think?

PHP Code:
function convert_to_utc($date$time$tz){

   $standard_timezone standard_timezone($tz);

   $timestamp  strtotime("$date $time");

   $system_timezone = new DateTimeZone('UTC'); // your timezone
   $user_timezone = new DateTimeZone($standard_timezone); // your user's timezone
   $now = new DateTime(date('Y-m-d H:i:s',$timestamp), $user_timezone);


   $utc_datetime $now->format('Y-m-d H:i:s');

   return array(
           "utc_time"=>$now->format('H:i A'),

PHP Code:
$da $this->convert_to_utc("07/02/15","9:06 PM","UM5"); 

PHP Code:
   [user_date] => 07/02/15
[user_time] => 9:06 PM
[utc_full] => 2015-07-03 01:06:00
[utc_date] => 2015-07-03
[utc_time] => 01:06 AM
[user_timezone] => UM5
[user_standard_timezone] => America/New_York


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.
To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.

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.

Theme © iAndrew 2016 - Forum software by © MyBB