Skip to content

Instantly share code, notes, and snippets.

@rrrhys
Created January 12, 2015 01:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rrrhys/485b16b542608ce2054e to your computer and use it in GitHub Desktop.
Save rrrhys/485b16b542608ce2054e to your computer and use it in GitHub Desktop.
Quick and easy readable -> mysql UTC -> readable again
User chooses date and time in picker.
Post to server:
var mom = moment(date).utc().format("YYYY-MM-DD HH:mm:ss");
$.post("/endpoint", {time: mom}, function(obj){
if(typeof(obj) != "object"){
obj = $.parseJSON(obj);
}
// put the UTC time in the field
$(".time-do").text(obj.schedule);
// mark it dirty
$(".time-do").addClass("to-local-time");
// convert all to-local-time's
utils.convertUTCsToLocalTime();
});
// utility code to convert UTC timestamps to user readable local time.
utils.convertUTCsToLocalTime = function(){
var utcs = $(".to-local-time");
$.each(utcs, function(idx, utc){
var loc = utils.convertUTCToLocalTime($(utc).text());
if(loc == ""){
$(utc).removeClass("to-local-time").text("--").addClass("invalid-date");
}
else{
$(utc).removeClass("to-local-time").text(loc).removeClass("invalid-date");;
}
//var mom = new moment($(utc).text() + "Z");
//$(utc).removeClass(".to-local-time").text(mom.local().format("DD/MM/YYYY h:mm a"));
});
}
utils.convertUTCToLocalTime = function(utc){
var mom = new moment(utc + "Z");
var result = mom.local().format("DD/MM/YYYY h:mm a");
if(result == "Invalid date"){
result = "";
}
return result;
}
// The controller (/endpoint):
$time = Input::get("time");
// date('Y-m-d H:i:s'); format
$run->relevant_time = $time;
$run->save();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment