UTC calcs

Costas

Administrator
Staff member
reference :
http://www.onlineconversion.com/unix_time.htm

rule : all calculation must done to client side...

JavaScript:
<script>
	$(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;
	}
</script>

<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
 
Top