How to automatically convert time zone

The date and time that are stored in my database, such as: - 2017-01-11 10:01:55 I want it to insert the same thing as I know, but while you are showing, I want it to be displayed as a time zone, for example, in India by the hours of Asia / Calcutta I used

date_default_timezone_set('Asia/Kolkata');
                $timestamp = date("Y-m-d h:m:i");

but it works if the insert code is not displayed, and I do not want to mention which time zone can be by default, it automatically determines the time zone and displays please help tried it also

 date_default_timezone_set('UTC');



 no success
+4
source share
1 answer

Just save your date in the UTC database, and then pass the date to a function in which it will return the date in your time zone.

$(function(){

  $('span.time').each(function(i, obj) {
     $(obj).text(convertDate($(obj).text()));
  });

});
function convertDate(givenDate){
    var myOldDateObj = new Date(givenDate);
    return myOldDateObj.toString();
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<b>Inputs:</b>
<div>
2017-01-11T12:14:11<br/>
2017-01-10T12:14:11<br/><br/>
</div>
<b>Output:</b><br/>
<span class="time">2017-01-11T04:11:33</span><br/>
<span class="time">2017-01-10T12:14:11</span>
Run codeHide result
+1

Source: https://habr.com/ru/post/1666490/


All Articles