Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWebsite Owner Tools

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Make System Time Human Readable

Depending on the project I'm working on, I may need to convert a server operating system time into a readable format. Perhaps frequently; again, depending on the project.

Thus, I've developed several ways to do it. Each method does the same thing — converts a 9- or 10-digit system time number into a day of the week name, the calendar date, and the time, all correctly formatted.

Do you need the software? Maybe.

If your log files or other documents contain 9- or 10-digit system time stamps, those time stamps can be converted into something that makes sense. Same with file names composed of a system time stamp.

Of course, it's nice to have a gadget around in case you get curious about what date and time a certain number would translate into. Or to impress someone with your tech expertise :-)

The different ways were developed as I worked with different programming languages.

  1. One is a CGI script written in Perl.
  2. Another is written in JavaScript.
  3. And a third is written in PHP.

Each works by either typing the number into a form field and clicking the button or by appending a "?" character to the URL followed by the system time number. The conversion is accurate to the second.

The source code of the complete script for each of the methods is further below.

If you wish to make your own applications with any of those languages, see the Date and Time Formatting article for a script generator to display your choice of formatted date and/or time.

The System Time to Human Readable Time Software

The source code of each of the three methods (with notes) are below. Each is complete software, ready to copy and paste. No customization required.

Information common to using each of the three methods is at the end of the article, after the source code sections.

The Perl CGI Method

Here's the CGI software written in Perl. Notes follow.

#!/usr/bin/perl
# 
#   Make System Time Human Readable
#   Version 1.0
#   September 20, 2014
#
#   Will Bontrager Software LLC
#   https://www.willmaster.com/
#   Copyright 2014 Will Bontrager Software LLC
#
#   This software is provided "AS IS," without 
#   any warranty of any kind, without even any 
#   implied warranty such as merchantability 
#   or fitness for a particular purpose.
#   Will Bontrager Software LLC grants 
#   you a royalty free license to use or 
#   modify this software provided this 
#   notice appears on all copies. 
#
use strict;
my $datetime = $ENV{QUERY_STRING};
$datetime =~ s/\D*//g;
print "Content-type: text/html\n\n";
print <<TOP;
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Make System Time Human Readable</title>
</head>
<body>
TOP
if($datetime > 2147483647)
{
   print '<p>Maximum system time number that ';
   print 'can be processed is 2147483647.<br>Your ';
   print "number $datetime has been reduced to ";
   print '2147483647.</p>';
   $datetime = 2147483647;
}
my $me = $0;
$me =~ m!([^/\\]+)$!;
$me = $1;
if($datetime)
{
   my @dt = localtime $datetime;
   my @weekday = qw( Sunday Monday Tuesday Wednesday Thursday Friday Saturday );
   my @month = qw( January February March April May June July August September October November December );
   $dt[5] += 1900;
   $datetime = "$weekday[$dt[6]], ";
   $datetime .= "$month[$dt[4]] $dt[3], $dt[5] ";
   $dt[2] = $dt[2] < 10 ? "0$dt[2]" : $dt[2];
   $dt[1] = $dt[1] < 10 ? "0$dt[1]" : $dt[1];
   $dt[0] = $dt[0] < 10 ? "0$dt[0]" : $dt[0];
   $datetime .= "at $dt[2]:$dt[1]:$dt[0]";
   print "<h3>$datetime</h3>";
}
print <<FORM;
<form action="$me" method="get">
<input type="text" name="timestring" style="width:150px;">
<input type="submit" value="Make Human Readable" style="width:150px;">
</form>
</body>
</html>
FORM
exit;

The Perl script is for Unix/Linux servers.

Viewing, editing, and transporting (uploading and downloading) Perl scripts should always be as plain text files, not as binary files.

To install and use the script:

  1. Verify the first line of the program points to the installation of perl on your server.

  2. Upload the script to a directory that can run Perl CGI programs, giving it a file name like HumanTime.cgi or something else with a .cgi or .pl file name extension.

  3. Give the script 755 permissions.

The Perl CGI version is used the same as the other two versions. Information is near the end of the article.

The JavaScript Method

Here's the JavaScript software. Notes follow.

<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Make System Time Human Readable</title>
</head>
<body>
<script type="text/javascript">
/*
   Make System Time Human Readable
   Version 1.0
   September 20, 2014

   Will Bontrager Software LLC
   https://www.willmaster.com/
   Copyright 2014 Will Bontrager Software LLC

   This software is provided "AS IS," without 
   any warranty of any kind, without even any 
   implied warranty such as merchantability 
   or fitness for a particular purpose.
   Will Bontrager Software LLC grants 
   you a royalty free license to use or 
   modify this software provided this 
   notice appears on all copies. 
*/
var milliseconds = 0;
if( location.search && location.search.indexOf('?') != -1 ) { milliseconds = parseInt(location.search.substring(12)); }
if( milliseconds )
{
   if(milliseconds > 2147483647)
   {
      document.write('<p>Maximum system time number that can be processed is 2147483647.<br>Your number '+milliseconds+' has been reduced to 2147483647.</p>');
      milliseconds = 2147483647;
   }
   milliseconds *= 1000;
   var now = new Date(milliseconds);
   var format = "{W}, {M} {D}, {Y} at {h}:{m}:{s}";
   var Wday = new String();
   var Warray = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
   Wday = Warray[now.getDay()];
   format = format.replace(/\{W\}/g,Wday);
   var Month = new String();
   var Marray = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
   Month = Marray[now.getMonth()];
   format = format.replace(/\{M\}/g,Month);
   var Mday = new String();
   Mday = now.getDate();
   format = format.replace(/\{D\}/g,Mday);
   var Year = new String();
   Year = now.getFullYear();
   format = format.replace(/\{Y\}/g,Year);
   var h = now.getHours();
   var hh = new String();
   if(h<10) { hh = "0"+h; }
   else { hh = h; }
   format = format.replace(/\{h\}/g,hh);
   var mm = new String();
   mm = now.getMinutes();
   if(mm<10) { mm = "0"+mm; }
   format = format.replace(/\{m\}/g,mm);
   var ss = new String();
   ss = now.getSeconds();
   if(ss<10) { ss = "0"+ss; }
   format = format.replace(/\{s\}/g,ss);
   document.write('<h3>' + format + '</h3>');
}
</script>
<script type="text/javascript">
document.write('<form action="'+document.URL+'" method="get">');
</script>
<input type="text" name="timestring" style="width:150px;">
<input type="submit" value="Make Human Readable" style="width:150px;">
</form>
</body>
</html>

To install and use the script, upload it to your server. Give it a file name like HumanTime.html or something else with a web page file name extension.

The JavaScript version is used the same as the other two versions. Information is near the end of the article.

The PHP Method

Here's the PHP software. Notes follow.

<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Make System Time Human Readable</title>
</head>
<body>
<?php
/*
   Make System Time Human Readable
   Version 1.0
   September 20, 2014

   Will Bontrager Software LLC
   https://www.willmaster.com/
   Copyright 2014 Will Bontrager Software LLC

   This software is provided "AS IS," without 
   any warranty of any kind, without even any 
   implied warranty such as merchantability 
   or fitness for a particular purpose.
   Will Bontrager Software LLC grants 
   you a royalty free license to use or 
   modify this software provided this 
   notice appears on all copies. 
*/
$datetime = preg_replace('/\D+/','',$_SERVER['QUERY_STRING']);
if($datetime > 2147483647)
{
   print '<p>Maximum system time number that ';
   print 'can be processed is 2147483647.<br>Your ';
   print "number $datetime has been reduced to ";
   print '2147483647.</p>';
   $datetime = 2147483647;
}
if($datetime) { echo '<h3>', date( 'l, F j, Y \a\t H:i:s', $datetime ), '</h3>'; }
?>
<form action="<?php echo($_SERVER['PHP_SELF']) ?>" method="get">
<input type="text" name="timestring" style="width:150px;">
<input type="submit" value="Make Human Readable" style="width:150px;">
</form>
</body>
</html>

To install and use the script, upload it to your server. Give it a file name like HumanTime.php or something else with a .php file name extension.

The PHP version is used the same as the other two versions. Information follows.

Using the Make System Time Human Readable Software

Type the URL of the web page into your browser.

When the page loads, you'll see a form for providing the system time number. Something like this:

After the form is submitted, the page will reload and print the human readable day of the week, calendar date, and time. There will be another form available for another system time number in case it's needed.

In addition to using the form as above, the page can also be launched with the system time number in the URL following a question mark character. Examples:

http://example.com/cgi-bin/HumanTime.cgi?1234567890
http://example.com/HumanTime.html?1234567890
http://example.com/HumanTime.php?1234567890

When done that way, the page will publish the human readable date and time and another form the same as it does when the form is used.

The article provides the system-time-to-human-readable software in each of 3 programming languages. Use whichever language you're most comfortable with — or the one you most want to learn more about.

(This article first appeared in Possibilities ezine.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC