Search code examples
phpcodeignitertimeunix-timestamptimestamp-with-timezone

Setting Codeigniter Timestamp in accordance to user Timezone settings


How can I record the timestamp in accordance to the user's timezone settings?

For example:

  1. The user has the timezone setting as $my_tz = "UP3";
  2. The user sets the deadline of an item as $time_to_set = "2017-05-31 03:15 PM";
  3. I need the users for example with different timezone settings to convert the time properly in accordance to their timezone (e.g. USER 1: UP3, USER 2: UP8). They need to see the time properly. I tried to set but the offset seems to have +1 hour difference and it is not accurate.
  4. I would need time set to the timezone (UP3) to be converted to the (UTC) base without going off by +1 hour.

Thank you so much!

Here is my code you can test to see what I mean.

    $my_tz = "UP3";
    $utc_time = local_to_gmt(time());
    $current_time = gmt_to_local($utc_time, $my_tz, FALSE);

    echo "Current Time: ".unix_to_human($current_time)."</br>";
    echo $current_time;
    echo "</br></br></br></br>";

    $time_to_set = "2017-05-31 03:15 PM";
    $time_set_unix = human_to_unix($time_to_set);
    $time_to_normal = local_to_gmt($time_set_unix, "UP8", FALSE);

    echo $time_to_set."</br>";
    echo $time_set_unix."</br>";
    echo $time_to_normal."</br>";
    echo unix_to_human($time_to_normal)."</br>";

Solution

  • I solved my issue. I ditched the CI Date Helper and opted for the default PHP one. It is much better than going through trial and error and always getting an offset that is way off the actual time.

    @astound's answer also helped in this.