SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot vargz's Avatar
    Join Date
    Apr 2003
    Location
    South Australia
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Page last updated/saved datestamp on web page?

    I'm trying to display a datestamp on each webpage, to show when it was last updated/saved.

    I'm using the following code:

    function pagedatestamp() {
    datUpdated = new Date(document.lastModified) ;
    datDate = datUpdated.getDate() ;
    datMonth = datUpdated.getMonth() + 1 ;
    x = datUpdated.getYear() ;
    var datYear = x % 100;
    datYear += (datYear < 38) ? 2000 : 1900;
    document.write(datDate + "/" + datMonth + "/" + datYear) ;
    }
    Which is called from a php footer include, but it seems to show the current date (which I think is being taken from the PC clock) instead of showing the date when the page was last saved/updated.

    How do I go about achieving this, in javascript or even with php?

  2. #2
    SitePoint Member SkinnyK's Avatar
    Join Date
    Sep 2008
    Posts
    20
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is a function that will help you out, maybe you can learn from it.

    http://www.quirksmode.org/js/lastmod.html
    Code JavaScript:
    function lastMod() {
    	var x = new Date (document.lastModified);
    	Modif = new Date(x.toGMTString());
    	Year = takeYear(Modif);
    	Month = Modif.getMonth();
    	Day = Modif.getDate();
    	Mod = (Date.UTC(Year,Month,Day,0,0,0))/86400000;
    	x = new Date();
    	today = new Date(x.toGMTString());
    	Year2 = takeYear(today);
    	Month2 = today.getMonth();
    	Day2 = today.getDate();
    	now = (Date.UTC(Year2,Month2,Day2,0,0,0))/86400000;
    	daysago = now - Mod;
    	if (daysago < 0) return '';
    	unit = 'days';
    	if (daysago > 730) 	{
    		daysago = Math.floor(daysago/365);
    		unit = 'years';
    	}
    	else if (daysago > 60) {
    		daysago = Math.floor(daysago/30);
    		unit = 'months';
    	}
    	else if (daysago > 14) {
    		daysago = Math.floor(daysago/7);
    		unit = 'weeks'
    	}
    	var towrite = 'Page last changed ';
    	if (daysago == 0) towrite += 'today';
    	else if (daysago == 1) towrite += 'yesterday';
    	else towrite += daysago + ' ' + unit + ' ago';
    	return towrite;
    }
     
     
    function takeYear(theDate) {
    	x = theDate.getYear();
    	var y = x % 100;
    	y += (y < 38) ? 2000 : 1900;
    	return y;
    }

    ..and calling it.
    Code JavaScript:
    document.write(lastMod())
    If you need someone to talk to, I'm always there to listen.

  3. #3
    SitePoint Zealot vargz's Avatar
    Join Date
    Apr 2003
    Location
    South Australia
    Posts
    161
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks but I already tried this - does the same thing but just puts it in terms of how many days ago the page was last updated.

    Anyone else know a way to achieve this?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •