Reputation: 149
I want to store my country date and time in JavaScript var variable using JQuery. That date and time should be in default mysql database datetime format. JQuery date and time should not be local date time and not for auto get timezone date time. That should be UTC +09:30 date and time in set.
PHP Code for that in mysql datetime format:
CONVERT_TZ(NOW(), 'UTC', '+09:30')
OR
Tell me how to convert JQuery NOW() date time data to mysql datetime format in PHP.
Final answer should be date and time in mysql datetime format and that should be in javascript var veritable or PHP veritable.
Upvotes: 0
Views: 1835
Reputation: 1839
Javascript date and time will be in local timezone.
If you want to display the time just like mysql time, then you will have to convert the localtime to server timezone in javascript.
You can you date.getTimezoneOffset()
, to get the offset between local time and utc, and then use this to convert locatime to utc+9:30
Edit
how to convert JQuery NOW() date time data to mysql datetime format in PHP.
Final answer should be date and time in mysql datetime format and that should be in javascript var veritable or PHP veritable.
Example to convert locatime to different timezone ( UTC + 9:30 )
var localDate = $.now();
localDate = new Date(localDate);
console.log("local datetime is ", printMysqlFormat(localDate));
var offset = localDate.getTimezoneOffset();
var offsetMilliseconds = offset * 60 * 1000;
var serverMilliseconds = localDate.getTime() + offsetMilliseconds + (570 * 60 * 1000)
var serverdate = new Date(serverMilliseconds);
console.log("localtime in server timezone is", printMysqlFormat(serverdate));
function printMysqlFormat(d) {
var day = d.getDate() + "";
var month = (d.getMonth() + 1) + "";
var year = d.getFullYear() + "";
var hour = d.getHours() + "";
var minutes = d.getMinutes() + "";
var seconds = d.getSeconds() + "";
day = day.length == 1 ? "0" + day : day;
month = month.length == 1 ? "0" + month : month;
hour = hour.length == 1 ? "0" + hour : hour;
minutes = minutes.length == 1 ? "0" + minutes : minutes;
seconds = seconds.length == 1 ? "0" + seconds : seconds;
return day + "-" + month + "-" + year + " " + hour + ":" + minutes + ":" + seconds;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
Upvotes: 1