|
-
Dec 26th, 2009, 02:18 AM
#1
[RESOLVED] Date last modified javascript
Hi,
I have the following javascript:
HTML Code:
// Add the page last modified date automatically to bottom of the page.
window.onload = setModifiedDate;
function setModifiedDate()
{
var dateMod = document.getElementById('datemod');
var text = document.createTextNode(document.lastModified);
dateMod.appendChild(text);
}
Which works but for some reason it changes the last modified date on my online websites if I just view the page in the browser.
Below is the code I am using in the html page:
HTML Code:
<script src="./javascript/extra.js" type="text/javascript"></script>
<div class="datemod">
<br/>
<p>Date last modified:</p><noscript><p>Copyright 2009</p> </noscript> <span id="datemod"></span>
</div>
Any ideas why this could be working incorrectly on my server?
Thanks,
Nightwalker
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Dec 26th, 2009, 02:35 AM
#2
Re: Date last modified javascript
the lastModified property is given by the headers sent to your browser. however, the last modified date is not a required header and so if it doesn't exist (eg. your webserver is not configured to send it), the current date would be defaulted (probably 0, or the current time). this also means that it is possible to configure your webserver to send this header. if you're on a shared host, you might not be able to do anything about that, however.
remember: javascript is a client-side language and thus has no idea what the real date modified would be for a file on any given server.
what you can do is either send this header yourself using a server side language (PHP, ASP, Perl, etcetera) and extract it using javascript, or use the server side language to output this information instead. I'd opt for the latter; I absolutely despise relying on javascript as the only method for actually displaying content.
-
Dec 26th, 2009, 04:07 AM
#3
Re: Date last modified javascript
Ah ok, thanks I will remember that for next time! Just checking my website now it has a last modified date of yesterday which was the date I last modified it so the problem seems to have been fixed. Although, I think the date was updating on my free webhost because since it was a free host they edit your html and add their own scripts, links, etc.
Anyway, will check in a day or so and post again if the last modified date changes when I haven't edited the code.
Last edited by Nightwalker83; Dec 28th, 2009 at 04:57 AM.
Reason: Fixed spelling!
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Dec 26th, 2009, 06:51 PM
#4
Re: Date last modified javascript
That is strange! All my websites except for the Bazaar Ceramics (html) are displaying their correct last modified date. However, the Bazaar Ceramics (html) is displaying the current date and time. Would the fact that this website is using php files instead of html files be causing problems with the javascript code. All the other sites I have used the code in post #1 on up until now have been straight html files.
Edit:
I am now experimenting with this php code to get the last modified date.
PHP Code:
<?php print "last modified "; print date("l, d F, Y [h:i:s]"); ?>
However, the time that is displayed is 90 minutes out from my local time! Is it possible to adjust this? If so how? At the moment I am just testing the script using Wamp server on my pc not on my server.
Found out how reading the php manual
I fixed the problem using:
PHP Code:
<?php date_default_timezone_set('Australia/Adelaide'); ?>
That is putting my location inside the quotes.
Last edited by Nightwalker83; Dec 26th, 2009 at 08:15 PM.
Reason: Adding more
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Dec 27th, 2009, 02:42 PM
#5
Fanatic Member
Re: Date last modified javascript
if you got an administrator site for example to edit the contents of your site, you can add an additional textbox that is hidden where it will contain the current date+time by doing it like this:
Code:
<input type="hidden" value="<?php print time(); ?>">
now that will store the current time into the database and with that you can just load it again and make it display <?php print date("l, d F, Y [h:i:s]", time()); ?> just instead when you load it make it load into a variable and replace the time() inside date with the variable name... this is how i would do it.
Last edited by Justa Lol; Dec 27th, 2009 at 02:46 PM.
-
Dec 27th, 2009, 07:41 PM
#6
Re: Date last modified javascript
 Originally Posted by Justa Lol
if you got an administrator site for example to edit the contents of your site, you can add an additional textbox that is hidden where it will contain the current date+time by doing it like this:
Thanks! Since I am already using a span for the last modified date I suppose I could put the code in there instead of adding the hidden text box to the pages. Well, the code I posted above isn't getting the last mod date/time as it is suppose to but instead retrieving the current date and time which is not what I need.
Edit:
Although, wouldn't that code just keep changing the date in the database according to the current date not necessarily the date/time I last modified the page, like the code I posted in post #4 is doing?
This code seems to solve problem of the date and time being the wrong date and time.
PHP Code:
echo " " . date ("F d Y H:i:s.", getlastmod());
I put in a separate file:
PHP Code:
<?php date_default_timezone_set('Australia/Adelaide'); echo " " . date ("F d Y H:i:s.", getlastmod()); ?>
and put:
PHP Code:
<?php include ("../php/lastmoddate/last_modified_date.php"); ?>
on the page where I want the date and time of the modification to appear.
Now I just need to test it tomorrow and see if it still has a date of:
if it does I will know the code works correctly.
Why does the date and time change when I upload the website to my web server?
The date and time on the uploaded website is:
December 29 2009 05:22:42
despite having set the timezone as above.
Last edited by Nightwalker83; Dec 27th, 2009 at 11:32 PM.
Reason: Adding more
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Dec 27th, 2009, 11:36 PM
#7
Fanatic Member
Re: Date last modified javascript
time(); is the current time which changes every second... but if you use echo time(); then it will print a number i think its 365 * 24 * 60 * 60 = 1 year(1971) basically, when you save what you edited you could save the value that is printed when you echo time(); then you could use that time(what you saved) and load it into a variable then use that variable in the date(); function...
date("F d Y H:i:s.", $var); now the $var is the variable you load the data you saved when you printed time(); which will display the time you saved/printed time(); and when you did that is the last time you modified..
-
Dec 27th, 2009, 11:42 PM
#8
Fanatic Member
Re: Date last modified javascript
i once made a guest book that would store a name, time, ip, mail and of course the message.. and when i saved the time i just stored time(); into the database and loaded it again when showing the comments by loading it into the date function, of course we in faroe islands and maybe most of Europe use a date function that looks like this d:m:Y h:i:s...
so i when i loaded this i just selected the row and put it in like this:
Code:
print "time added: " . date('d-m/Y h:i:s', row['date']) . ".";
-
Dec 27th, 2009, 11:45 PM
#9
Re: Date last modified javascript
 Originally Posted by Justa Lol
time(); is the current time which changes every second... but if you use echo time(); then it will print a number i think its 365 * 24 * 60 * 60 = 1 year(1971) basically, when you save what you edited you could save the value that is printed when you echo time(); then you could use that time(what you saved) and load it into a variable then use that variable in the date(); function...
date("F d Y H:i:s.", $var); now the $var is the variable you load the data you saved when you printed time(); which will display the time you saved/printed time(); and when you did that is the last time you modified..
Ah ok, thanks! Do you know why the date/time would be different on the uploaded site to when I test it on my local machine. The code I'm using keeps the numbers of seconds between the last mod time as it was on my local machine just the time is different on the uploaded website.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Dec 27th, 2009, 11:48 PM
#10
Fanatic Member
Re: Date last modified javascript
it might be because the time is loading from the systems or servers time?
edit: well, it could be the servers time is a bit of your time so they don't really match...
Last edited by Justa Lol; Dec 27th, 2009 at 11:52 PM.
-
Dec 28th, 2009, 03:43 PM
#11
Re: Date last modified javascript
Had a look at the date last code from both the online site and my local version and they produced the results I was looking for from post #6. The code works great!
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
-
Dec 29th, 2009, 11:35 AM
#12
Re: [RESOLVED] Date last modified javascript
err. I'm not sure either of you really have a great understanding of timestamps or the date() function.
Justalol, he was already doing that by calling getlastmod() as the second parameter in the date() function. you don't need to store the date in a database for a site that isn't even database driven in the first place. if you're using text files/includes to "build" your website then storing one thing in the database really isn't all that helpful. if you're storing everything (meaning, all of your content) in the database, then it makes a lot of sense to do that. otherwise, getlastmod() will get the last modified date of the current file. if you're using includes, however, you would have to use the filemtime() function to get the last modified date of the include.
anyway, for clarification -- the time() function returns a unix timestamp. this is the amount of seconds past since the Unix Epoch (January 1, 1970, 00:00:00 GMT). the date() function can use any unix timestamp to produce a properly formatted date.
and, lastly, the last modified date for your file will not be the same on your server as it is on your computer. the last modified date on the server will always be the last time you uploaded it, rather than the last time you actually modified the file. your server's filesystem and your own filesystem are completely independent of each other, and as such when you upload the file to the server you're essentially creating a new file (thus, the modified time is reset). this could be the reason for your problem, but may not be.
-
Dec 29th, 2009, 03:40 PM
#13
Fanatic Member
Re: [RESOLVED] Date last modified javascript
i didn't say thats how he should do, thats how i do it... i once made a site for another person that needed to post news and in there i made it add the post date that way, and yes i got understanding in this..
-
Dec 29th, 2009, 06:07 PM
#14
Re: [RESOLVED] Date last modified javascript
 Originally Posted by kows
and, lastly, the last modified date for your file will not be the same on your server as it is on your computer. the last modified date on the server will always be the last time you uploaded it, rather than the last time you actually modified the file. your server's filesystem and your own filesystem are completely independent of each other, and as such when you upload the file to the server you're essentially creating a new file (thus, the modified time is reset). this could be the reason for your problem, but may not be.
Unless you modify a page while it is on the host, that is using the editor the host provides. This is what I was doing and the resulting last mod date and time ended up matching that of the current data and time on my local machine. The reason for the offset in date and time as posted above was likely to be because they were doing work on the server at the time.
when you quote a post could you please do it via the "Reply With Quote" button or if it multiple post click the "''+" button then "Reply With Quote" button.
If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
https://get.cryptobrowser.site/30/4111672
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|