"Timestamps" -- Date & Time


the types TIMSTAMP, DATE, TIME & INTERVAL are interrelated. Timestamp includes the full idea of a moment in time, DATE is just rounded off to the start of a day, while TIME is the part within a day. You can subtract two of these, to get a time INTERVAL. You can also add (or subtract) an interval from a Time value.

The standard output format for DATE is '2010-03-15' reasonable input formats are accepted, including the former and '15 Mar 2010', and whatever it means, '3/4/2010' or ' 03/04/15' (not recommended!)

Examples of Interval inputs: '2 years 1 month 4 days 10 hours'  '360 days' output will only show days and time, since months (and years) vary in length. With DATE, you can add integer days.
You may have to use type casts in expressions, such as: CURRENT_DATE + '2 months' :: INTERVAL or '25 Dec 2010' :: DATE - CURRENT_DATE
Remember that the result php receives is a string and not necessarily an integer. The last expression above returns integer days, but look at this:

select '25 Dec 2010 12:30' - now();
285 days 14:53:53.06545


PHP uses a basic unit of time of one second and UNIX style timestamps: The number of seconds since 1 January 1970, GMT. The current value of this is is an integer, returned by time() and used by date() and setcookie() - for the "expire" time.
For other times: strtotime -- Parse about any English textual datetime description into a Unix timestamp
For formatting with date, see examples below or consult the PHP referene.
For setting the expire time of a cookie, the third argument (after name, value,) should be time() + a number of seconds,
setcookie("TestCookie", $value, time()+3600);  /* expire in 1 hour */


The basic unit of time is millisecond.  Javascript does not have such nice formating functions as PHP. Instead, it has a Date object, which "hides" the details of storage, and provides methods to access various aspects of a date, such as getDate, getMonth, and getFullYear. These retrun integers for the day-in-month, month, and year(4 digits). But careful! getMonth returna a number from 0-11. There are various constructors to create a Date. Date() gives you "now", Date( string ) converts a reasonable string to a date. Both the GMT style (see below) and that used in lastModified are acceptable. For example, to format lastModified nicely, I have used:
var md = new Date(document.lastModified)
var months = new Array ("January","February","March","April","May","June","July",
document.write("Last updated: "+ md.getDate()+" " + months[md.getMonth()]
+ " " + md.getFullYear())

Example: Last Modified date/time of a page

HTML, using Javascript

This is easy. The web server senda a header, Last-Modified, with a date and time in "GMT" format, for example:

Last-Modified: Wed, 03 Feb 2010 19:58:16 GMT

This information is stored in document.lastModified, so can be printed, or perhaps better formatted, using JavaScript. Unfortunately different browsers use different formats, which you may or may not like. I have written a function, in LastUpdate.js, which Formats the date nicely. You may use it. If Javascript is disabled, nothing will be written. The "noscript" is of course optional.

Last modified
<script type="text/javascript">
<script src="/ljensen/LastUpdate.js"></script>
<noscript><i>see "Page Info"</i></noscript>

Last modified 01/21/2010 16:27:00
Last updated: 21 January 2009


The above method is useless for a PHP file, since each request generates new html. The header is not sent, we will either get nothing or current date/time from Javascript. However, PHP can find lastModified in the filesystem, the function is getlastmod(), and print it for you (use whatever date() format you prefer):

Script last modified 
print date ("j M Y H:i", getlastmod());

Script last modified 25 Feb 2008 16:10

For code that is included such as my "footer" file, we need the time of the main file, to be found in $_SERVER['SCRIPT_FILENAME'], since clearly that is more meaningful than the date the footer code was last updated.

Last modified
$mainfile = $_SERVER['SCRIPT_FILENAME'];
print date("d F Y",filemtime($mainfile));

This file last modified