reference : http://www.onlineconversion.com/unix_time.htm
rule : all calculation must done to client side…
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
$(function () {
//init with date now
$("[name=post_date]").val(date_formatter(new Date()));
//edit button - read record
function query_POSTS_modal(rec_id){
$.ajax(
{
url : "tab_posts_fetch.php",
type: "POST",
data : { post_id : rec_id },
success:function(data, textStatus, jqXHR)
{
if (data!='null')
{
$('[name=post_date]').val(getGMTfromUTC(data.post_date));
console.log(getGMTfromUTC(data.post_date));
$('#lblTitle_POSTS').html("Edit Post");
$('#modalPOSTS').modal('toggle');
}
}, error: function(jqXHR, textStatus, errorThrown) {}
});
}
////////////////////////////////////////
// MODAL SUBMIT aka save
$('#formPOSTS').submit(function(e) {
e.preventDefault();
var postData =form.serializeArray();
var formURL = form.attr("action");
var dt = parseDate($("[name=post_date]").val());
var post_date_utc = getUTCfromGMT(dt);
//add date as UTC
postData.push({name: "post_date_utc", value : JSON.stringify(post_date_utc)});
$.ajax(
{
url : formURL,
type: "POST",
data : postData,
success:function(data, textStatus, jqXHR)
{ },
error: function(jqXHR, textStatus, errorThrown)
{ }
});
});
}); //jQ
function date_formatter(value) {
if (value==null)
return "";
var date = new Date(value);
var options = {
year: "numeric", month: "2-digit",
day: "2-digit", hour: "2-digit", minute: "2-digit", hour12: false
};
return(date.toLocaleString("en-GB", options));
}
function parseDate(dt_txt) {
dt_txt= dt_txt.replace('T',' ');
var data = dt_txt.split(' ');
var datePart = data[0].split('/');
var timePart = data[1].split(':');
// new Date(year, month [, date [, hours[, minutes[, seconds[, ms]]]]])
return (new Date(datePart[2], datePart[1] - 1, datePart[0], timePart[0], timePart[1]));
// months are 0-based
}
function getUTCfromGMT(val) {
var newDay = new Date();
var t = new Date(val);
var time1 = Math.round(t / 1000);
return (time1);
}
function getGMTfromUTC(val) {
var d = new Date();
d.setTime(val * 1000);
var hours = d.getHours();
var minutes = d.getMinutes();
if (hours < 10) {
hours = "0" + hours;
}
if (minutes < 10) {
minutes = "0" + minutes;
}
//http://www.w3schools.com/jsref/jsref_getmonth.asp
// months are 0-based
return d.getDate() + "/" + (d.getMonth() + 1) + "/" + d.getFullYear() + " " + hours + ":" + minutes;
}
<input type="text" name="post_date" class="form-control" placeholder="dd/mm/yyyy hh:ii">
so the user is on GMT+2 and inputs 22/03/3000 16:43 (aka in UTC is 32510644980 (22/03/3000 14:43 because of user GMT+2))…
1-when submit the form @ line 38, converts the datetime^ to UTC and add it to POST OBJ 2-this goes to PHP where save it to a dbase 3-later user need to retrieve this data, php returns by dbase the 32510644980 back to JS 4-@ line 16 convert it back to GMT+2 date (22/03/3000 16:43) :)
The advantage : if the user moved to Australia (GMT+10) will show 23/03/3000 24:43
origin - http://www.pipiscrew.com/?p=3537 js-utc-calcs