Forum Moderators: mack

Message Too Old, No Replies

topic posted x hours and x minutes ago feature..

how to implement

         

krez

10:07 pm on Jul 16, 2005 (gmt 0)

10+ Year Member



Hello every body first time poster and don't know if i am posting in the right section but here goes...i am searching for a script for when posting articles on my website i would like to have the feature that various websites use on displaying the time it is posted.

For example if a site is updated with a fresh new article you would see...posted 1 minute ago and if you would return 3 days later you would see.....72 hours and 5 minutes ago.

I think you know what i mean, it is something that is widely used and would also like to have this kind of feature on my site but don't know how to do this or what kind of script i need to use for this, don't even know how such a script is called.

Hope somebody knows how i can do this?
Thanks in advance guys.

MichaelBluejay

6:37 am on Jul 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



It depends on how people are "posting" articles. If they're posting with some kind of content management system software (CMS), then the CMS will have to have that feature included, or a programmer will have to add it for you.

jatar_k

7:11 am on Jul 20, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld krez,

easy enough to do though as MichaelBluejay mentioned it depends on how they are posted. You would need a date and time attributed to the post/article itself and then compare that with the present time. If you use a scripting language of some sort now any of them can handle this. It isn't a script unto itself it would be a small feature of one.

krez

9:29 am on Jul 20, 2005 (gmt 0)

10+ Year Member



Thanks for the reply guys, the articles are going to be posted by myself and i have been looking around and have found some information on this subject.

Now i know and correct me if i am wrong that i need a client and a server side script for making this possible.
What would be the best script language to use regarding my inexpierence with script languages?
I have a piece of javascript that i could include in my website page itself, the client side so to speak but i need another sort of script language to make it combine with each other right?
The following is the javascript that i have found:

<script language="javascript">
function difference(oldtime) {
var nowtime = new Date();
var milliseconds1 = oldtime.getTime();
var milliseconds2 = nowtime.getTime();
var difference = milliseconds2 - milliseconds1;
var daysDifference = Math.floor(difference/1000/60/60/24);
difference = difference - daysDifference*1000*60*60*24
var hoursDifference = Math.floor(difference/1000/60/60);
difference = difference - hoursDifference*1000*60*60
var minutesDifference = Math.floor(difference/1000/60);
difference = difference - minutesDifference*1000*60
var secondsDifference = Math.floor(difference/1000);
// This next line below looks for entries over a day old
if (daysDifference > 1) document.write(' &nbsp\;<span class="lastupdated"><b>' + daysDifference + '</b> days, <b>' + hoursDifference + '</b> hours,<b> ' + minutesDifference + '</b> minutes ago</span>');
else if (hoursDifference < 1) document.write(' &nbsp\;<span class="lastupdated"><b>' + minutesDifference + ' </b> minutes ago</span>');
else if (hoursDifference < 4) document.write(' &nbsp\;<span class="lastupdated"><b>' + hoursDifference + ' </b> hours, <b> ' + minutesDifference + ' </b> minutes ago</span>');
else if (hoursDifference < 24) document.write(' &nbsp\;<span class="lastupdated"><b>' + hoursDifference + ' </b> hours ago</span>');
else document.write(' ');
}
</script>

And this is the php script that i can use for the server side that somebody put together for me:

<?
function elapsed_time ( $start, $end )
{
$diff = $end - $start;
$days = floor ( $diff/86400 ); //calculate the days
$diff = $diff - ($days*86400); // subtract the days

$hours = floor ( $diff/3600 ); // calculate the hours
$diff = $diff - ($hours*3600); // subtract the hours

$mins = floor ( $diff/60 ); // calculate the minutes
$diff = $diff - ($mins*60); // subtract the mins

$secs = $diff; // what's left is the seconds;

return "Article was posted ".$days." days, ".$hours." hours, ".$mins." minutes and ".$secs." seconds ago.";
}

$post = 1121642445; // this would be the time stamp you get back from the database
$now = mktime(); // time now

echo elapsed_time ( $post, $now );
?>

I am very confused if these codes are correct and on how to implement them to make everything work in the manor i expect.
If i make a change on the page in question that has articles being placed on, would the javascript and php script above combined work to display a "posted ago" message with the brief description of the content of the article.
And how do i add other content without displaying the "posted ago" message?
Because at this point to my knowledge if i implement these scripts then everything that i add will contain a "posted ago" message, and i only want the message display for the news articles.

I am planning to use a piece of software that i can use for adding articles and the brief article descriptions on the home page, it works by e-mail, i can add new content by just sending a e-mail and then the content is being placed on the website, if the software does what is says:) that is, i have not tried it out yet but will see if this works for me.

MichaelBluejay

10:19 pm on Jul 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Actually I guess you could do the entire thing with Javascript. Here's the basic structure:

---------

<HTML><HEAD>
<script type="text/javascript">
function printElapsedTime(startingDate) {
(code to figure the elapsed time goes here)
document.write('Posted ' + elapsedTime + ' days ago');
}
</script>
</HEAD>
<BODY>
<P>blah blah blah
<script type="text/javascript">printElapsedTime('7-20-05')</script>
<P>blah blah blah
</BODY>
</HTML>

-----------------------------

When you post a new article, just change the '7-20-05' to the date of your choice.

Preferably you should also put the printElapsedTime function in a separate file.

If you need more help I'd take this to the Javascript forum.

krez

9:13 am on Jul 22, 2005 (gmt 0)

10+ Year Member



Thanks MichaelBluejay for your assistance, i would actually go deeper on this, because it's still kind of blurry to me:(

Seperate file? How do i do this?

Also when posting a new article, i don't understand how i can make this work for every article that is going to be placed?, How do i merge the article and the script together so the script can do it's job?
I have a lot of questions, and i think with every answer i have a question, i really hope you don't mind?
I'm glad somebody is helping me out on this!

MichaelBluejay

3:58 am on Jul 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Like I said, if you need more help, take it to the Javascript forum.

I'm not on these boards very much so I may or may not see your post there, but that's the best place for it.

krez

9:42 am on Jul 27, 2005 (gmt 0)

10+ Year Member



Yes, i followed your advice but no one seems to know how to do this or want to reply to my post in the javascript section, i did give it a new title "something nice to add to a news article", i find it very interesting if this all can be done with javascript, saves a lot off hassle with PHP programming that is like speaking marsian for me:)

I can understand if you are busy but hope someone else can jump in someday and also offcourse you have the time someday to discuss this with me:)

nigassma

4:52 pm on Jul 27, 2005 (gmt 0)

10+ Year Member



Something to think about when using javascript.... whenever you use javascript you have to also think about the users that do not have javascript turned on. If they have it turned off do you care whether or not they see the date and time? Scripting should be kept server side if at all possible.

krez

7:00 pm on Jul 27, 2005 (gmt 0)

10+ Year Member



Good point, but do i care? no not really, most people i guess have javascript turned on.
And if not then, oh well, at this point i find this all hard to realize taking PHP to work on this subject.

Installing everything, learning how to update via database and then installing scripts, if i want to do this, i can maybe start next year putting the site online , sounds like i am really thinking i am over my head with PHP, doesn't it?, your right i do feel like this:)
All those command strings are scaring the **** out of me:(
It all sounds very complicated, therefor i thought javascript sounds like a good option:)

nigassma

7:19 pm on Jul 27, 2005 (gmt 0)

10+ Year Member



but do i care? no not really

You should care if you care about your site visitors

most people i guess have javascript turned on.

They key phrase being "i guess". Many corporations, businesses, schools, etc. have network administrators that force their users to turn off javascript. That could cause issues if you wanted anyone to navigate your website from their place of work.

krez

9:28 pm on Jul 27, 2005 (gmt 0)

10+ Year Member



The message posted ago that combines with the article is a nice feature to have and not a must have for the visitors, it's a nice add on, that i would like to see.
Lot's and lot's of websites rely their function on javascript, businesses for the most of the time don't allow private surfing so therefor they couldn't even reach my site, schools are not the main public that visits websites, sure they visit websites on classes, but hey these restrictions for not having javascript turned on would also mean that a lot of those websites designed for youngsters don't work, and they are especially designed for the people that still goes to school.

Like i mentioned, it's for the time ago posted message that i would like to combine with the articles, nothing more:) but i do understand your point, that you have to take in considderation all kind off possibilities that would cause the main function of a website to fail, so you can anticipate on it, but this doesn't count for me apart from the posted ago message with the articles.
But would not cause problems for the visitor, you see i do care about the visitors, afterall this is what it makes worth while, the only thing is my problem with PHP, therefor i choose to have a simpler option and in due time maybe i can look into the option for someone to help me out with this, some sort of agency?

MichaelBluejay

12:51 am on Aug 3, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, i followed your advice but no one seems to know how to do this or want to reply to my post in the javascript section, i did give it a new title "something nice to add to a news article"

Wow, and you actually thought that was helpful? Here's a rule of thumb: If some has to OPEN your post before they have a clue as to what it's about, then it's got a bad title. Really, that's just one step above the completely useless "please help" and "question". It's at about the same level as the slightly less useless "question about [some topic]".

You should care if you care about your site visitors

This is just ridiculous. As though you're punishing the <1% of visitors if they don't see some extra text that they wouldn't see anyway had you not installed the Last Updated script. As though you should bend over backwards to learn server-side scripting just so a tiny tiny tiny tiny minority of users can have access to what's a superfluous feature anyway.

Worry about Javascript whe the lack of it would BREAK your site, not when it effectively makes NO IMPORTANT DIFFERENCE WHATSOEVER.

The other thing is that often those who preach about catering to the 1% of visitors are quite happy to punish the other 99%. I design dynamic E-commerce sites that make browsing and ordering products a breeze for the 99% of users with Javascript -- a much better experience than they'd get on typical sites. The 1% of the users without Javascript don't get those features. Many purists wouldn't design my style of site because it punishes 1% of visitors, although NOT designing a nice dynamic site punishes 99% of them.

If it's easy for you to add a feature that enhances the experience of 99% of your visitors, do you do so, or do you hold out on them just because that feature won't work for everyone? This should be no contest.

Okay, Krez, you asked about how to put Javascript in a separate file. Just put all the Javascript code in a text file, say "javascript.js", and pull it into your HTML document like so:

<HTML><HEAD>
<SCRIPT type=text/javascript src="javascript.js"></SCRIPT>
</HEAD>

That keeps your HTML document nice and tidy. Also, if you need to change your Javascript code later on, you can change it in one place, rather than having to update a script that's hard-coded into every page.

In your text file just have the functions themselves; don't include the <SCRIPT></SCRIPT> tags in the text file.

As to how to make it work for every article placed, in msg#5 I showed you the rough outline. Every time you post an article, you'll need to update the date that's listed. In my example it says 7-20-05. Change as necessary.

krez

11:25 pm on Aug 5, 2005 (gmt 0)

10+ Year Member



Thanks i will work on it and see how it works out, and yes you are absolutely right about the topic description i gave my question, useless as you said. Thanks for the eyeopener:)