3

The date and time stored in my database is as:- 2017-01-11 10:01:55 I want it to insert same as know but while displaying I want it to display as timezone like in India by Asia/Kolkata timezone I have used

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

but it works in case of insert code not display and i don't want to mention which timezone can it be by default it automatically detect timezone and display acoordingly please help have tried this also

 date_default_timezone_set('UTC');



 no success
Ghugu
  • 479
  • 4
  • 11
  • You can use javascript if you want to show in web. Store the data in database in UTC time and then get timezone of user like this `var d = new Date(); var timezone = d.getTimezoneOffset();`, Then change the date according to timezone check this link http://stackoverflow.com/questions/10087819/convert-date-to-another-timezone-in-javascript#answer-18612568 – Aman Rawat Jan 11 '17 at 11:33
  • please can send me the JS code that will be great m new in web development – Ghugu Jan 11 '17 at 11:36
  • 1
    Database dates don't have a "format" (unless you're doing it wrong and you're storing it as text). Key point is whether they have a time zone and that vastly depends on the DBMS you use. In any case I think you're confusing two entirely different issues: 1) How to manipulate dates in your app 2) Detect you user's time zone – Álvaro González Jan 12 '17 at 10:16

1 Answers1

1

Just store your date in database UTC and then pass the date to the function it will return you the date in your timezone.

$(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>
Aman Rawat
  • 2,625
  • 1
  • 25
  • 40