Retrieve date and convert it to specific time zone

2020-08-01 05:25发布

i want retrieve date from database(s_start) and convert it to Specific time zone like (Africa/Cairo) according to user time zone from database (s_timezone) i can only retrieve orginal date stored in database and i can't convert the result My controller code :

$this->db->select('s_id');    
$this->db->select('s_start');    
$this->db->from('sessions');

$query = $this->db->get($this->event);
    if ($query) {
        return $query->result();
    }
    return NULL;

enter image description here

html code :

          <div id="cal-slide-content" class="cal-event-list">
          <ul class="unstyled list-unstyled">
        <% _.each(events, function(event) { %>
        <li>
            <span class="pull-left event <%= event['s_start'] %>"> 
         </span>&nbsp;
            <a href="" data-event-id="<%= event.id %>"
                data-event-class="<%= event['class'] %>" class="event- 
              item">
                <%= event.s_start %>&nbsp;<%= event.u_timezone %>&nbsp; 
                (<%= event.u_name %>)&nbsp;
                 </a>

      </li>
       <% }) %>
   </ul>

}

1条回答
唯我独甜
2楼-- · 2020-08-01 05:53
$date = new DateTime($result->s_start, new DateTimeZone($result->s_timezone));
$date->setTimezone(new DateTimeZone('Africa/Cairo'));
echo $date->format('Y-m-d H:i:sP') ;

Resources

Edit A function in the controller to convert timezones.

public function _convert_time($result){
    $date = new DateTime($result->s_start, new  DateTimeZone($result->s_timezone));
    $date->setTimezone(new DateTimeZone('Africa/Cairo'));
    return $date->format('Y-m-d H:i:sP') ;
}

Now you can echo the result

foreach($results as $result){
    echo $this->_convert_time($result);
}
查看更多
登录 后发表回答