PCinvent.info

PCinvent Studio Template

Jun 16 2008
Deal with Date & TimeStamp Conversion for Web Programming Print E-mail
Monday, 16 June 2008


Read More Articles in: Technology>>>Programming

This Article is written by This e-mail address is being protected from spam bots, you need JavaScript enabled to view it


Unix timestamp format is common seen when we need to set the date, such as creation date, modification date, available date or expiration date. Very often, we will use the time() function to generate the timestamp, for example, 1209441600.
However, when we design the form for entry or output the result to user, the timestamp format is def. not the one human can read.
To deal with Time Stamp and Date, here is the article I wrote to introduce server side and client side time stamp conversion......

 

PHP Server Side

In this case, we had a time stamp variable $cdate (Creation Date),  its integer value is 1209441600.
to convert that to date, we do:

<?php echo date("m/d/Y H:i",$cdate);?>

Note that m/d/Y H:i is the format for output, you can certainly change the order to fit what you need.

Very Importantly, uppercase Y will output yyyy, let say 2008. Lowercase y will output yy, let say 08.


JavaScritp Client Side

Sometimes, we might need to convert the timestamp in a form, in that case, a convertion will be needed since JavaScript doesn't has build-in function for that.

Here is the JS function I wrote to handle conversion on both side of Unix timestamp and date:

 
      <Script language="JavaScript" type="text/javascript">
  //The code below is written by Andy Ng<www.pcinvent.info>
  //Please keep the above credit line to the Author
  function stripZeros(input) {
  return input;
  if((input.length > 1) && (input.substr(0,1) == "0")) {
  return input.substr(1);
  } else {
  return input;
  }
  }
  function timestampTodate() {
  var theDate = new Date(document.unixtime2date.unixtimestamp.value * 1000);
  dateString = theDate.toGMTString();
  document.unixtime2date.output.value = dateString;
  }
  function dateToTimeStamp()
  {
  var humDate = new Date(Date.UTC(
  document.date2unixtime.vyy.value,
  (
  stripZeros(document.date2unixtime.vmm.value)-1),
  stripZeros(document.date2unixtime.vdd.value),
  stripZeros(document.date2unixtime.vhh.value),
  stripZeros(document.date2unixtime.vmin.value),
  stripZeros(document.date2unixtime.vsec.value)
  )
  );
  document.date2unixtime.output.value = (humDate.getTime()/1000.0);
  }
  <script>

 

Comments (0)Add Comment

Write comment
quote
bold
italicize
underline
strike
url
image
quote
quote
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley
Smiley

busy
Tags:  Technology Programming Deal with Date & TimeStamp Conversion for Web Programming Time Stamp to Date Date to Time Stamp Unix Time to Date Date to Unix Time Time Stamp TimeStamp tool Time Stamp tool time stamp conversion
Last Updated ( Tuesday, 17 June 2008 )
 
< Prev   Next >
Site by PCinvent.com