SO dates calculation and formatting - php

What script SO uses for dates displaying? Because it seems to be pretty nice formatting and logical showing.

I am not sure what StackOverflow use. But one of the most common timestamp representaion is the one done by twitter which displays timestamp as a moment ago, 30 seconds ago, x minutes ago, yesterday, 10:30 PM Apr 12, 2010 etc. And it updates the timestamp every five seconds without hitting the server.
If you are interested you may look into John Resig's Pretty Date JS API. It's just awesome. Works with/without JQuery.
We tweaked it a bit to exactly match Twitter pattern. And it is awesome.
The website mentioned gives good example, but if you so want a working version, put the following script in address bar of any web-page. (you may want to tweak parameters passed to prettyDate function
javascript:var i,s,ss=['http://ejohn.org/files/pretty.js'];for(i=0;i!=ss.length;i++){s=document.createElement('script');s.src=ss[i];document.body.appendChild(s);}alert("PrettyDate: "+(prettyDate("2011-03-13T03:24:17Z")?prettyDate("2011-03-13T03:24:17Z"):"03:24 AM Mar 13, 2011"));

I think it's based on the answers to this question on Stack Overflow asked by Jeff Atwood - most of the answers are in c# but there is a PHP implementation too

Related

PHP Relative Time

Hello I have a question about relative time formats in PHP. I am looking to get the time "month to date." For example, Today is May 5th, I would like to get the time span from May 1st, to May 5th. I tried using the format "this month," but I did not have success. Can anyone point me in the right direction?
Will strtotime() work for you? It takes a string such as next month and then creates a timestamp of it.
If not, take a look at this Stack Overflow question.
Another option would be using DateTime which is builtin to PHP as well.

Twitter statuses timestamp help

I'm trying to get twitter statuses displaying on my blog, however I cannot get the time each status is created at to display the way in which I desire. Here is how it is being printed now:
Thu Aug 05 12:36:20 +0000 2010
However I would like it to be displayed like this:
54 days ago
How can I manage this with PHP preg_replace?
Also at the moment I am using the twitter API to get the statuses. Is it better to use this method or an RSS feed? I would appreciate if anyone could help me out. Thanks
I would really recommend writing it out to the page as "Aug 5 2010" (or however you want it to appear). That way you only need to write it out once ever, not once per day. But also write as a GMT timestamp in a way that JS can read it but people that have JS turned off cannot see it. Then, once you've got your page displaying things correctly, use a JS script to loop through the tags and replace the dates with the friendly text you want. Example:
<span class="dateToBeReplaced" title="Thu Aug 05 2010 12:36:20 GMT+0000">Aug 05 2010</span>
The JS would look something like this (uses jQuery): http://jsfiddle.net/JxTLt/4/
JS is a little more finicky about date formats than PHP, so you pretty much should stick the the format above. Use strtotime() to handle the formatting and time zone conversion.
You don't need to manually parse the date, it's already in an understandable format. If you run it through strtotime it will return a timestamp that you can work with.
The concept of displaying time as you want is called "fuzzy time", and you can find a really code post on it here.
You won't be able to do this with preg_replace() alone. You need to do some temporal comparisons to create a human readable timestamp. Check out this post. Also keep in mind that Twitter responses will be GMT.
As far as the API vs. RSS, this is really up to you. Both responses have to be parsed. There is arguably more overhead with the API now that Twitter only supports OAuth. Although there are several PHP libraries available. If you only want to display statuses, I'd go with the RSS.

display time in a web page

I have page where where I list all comments for a post. Next to each comment I have a time value in full format including the date/time (2010-01-02 11:11:20).
I know that I can format it in PHP before displaying it, so it shows;
posted 40 secs ago
posted 5 days ago
but that would not be efficient as I am going to cache the page once it is generated.
On SO I see that they have some kind of java script for showing it, so it is run on the browser of the client:
alt text http://img528.imageshack.us/img528/5442/35118769.png
So basically I need a java script that runs on the browser that would instead of showing my date/time show "posted 4 hours ago". All help is welcome.
Sounds you want this: timeago: a jQuery plugin.
Timeago is a jQuery plugin that makes it easy to support automatically updating fuzzy timestamps (e.g. "4 minutes ago" or "about 1 day ago").
I quote further:
Avoid timestamps dated "1 minute ago" even though the page was opened 10 minutes ago; timeago refreshes automatically.
You can take full advantage of page caching in your web applications, because the timestamps aren't calculated on the server.
So it should fit your needs.
You can put the "timestamp" of the page into the page when you render it. This is the fixed time that doesn't change. Look at PHP's time function for this (http://www.php.net/manual/en/function.time.php). It gives you seconds since the UNIX epoch started.
Then, when the page is running, use Javascript's Date object. It encapsulates something similar. Do a getTime() on a new Javascript Date object, which will get you also seconds since the UNIX epoch.
Subtract one from the other to get the elapsed time between then and now, and do whatever pretty formatting you need to make it display right in your page.
Edit: See also Felix's answer about timeago, which is a nifty tool to do the second half of this process for you automatically.
Output your date in a format accepted by the parseDate method or as a constructor parameter for date. Place them in some nodes which can later on easily be grabbed by a javascript framework (or your own methods) and then perform some time/datediff methods on these grabbed values and replace the node's content.

want to show a time for the comment last posted through php

I want to show when the comment last posted in PHP. like 5 minutes ago, 2 days ago, 7 weeks ago. How to do this?
You can find plenty of answers with full solutions in different languages, pseudocode, ideas, etc.. here.
I believe there's an example of PHP too.
You can use timeago, a jQuery plugin, to do it via Javascript. It would yield the same result, update without refreshing, and by doing it client side instead of server side, you are not precluded from caching.
http://timeago.yarp.com/
Otherwise, Annurag has a link with some good PHP solutions.
You can do manual calculation in server, to get the time difference, then translate it into human time format.
Or my preference, do it in browser using javascript. Using this approach, the time in page can be updated without refresing the page.
You can use this jQuery EasyDate library to translate a DOM element into humane time format.
You can also read the comments in this post about Pretty Date by John Resig. It contain the code, and improvement by other.
Store the comment posted in the date DB and show the the same in the front end by comparing with current date and time using php function

How to create a friendly date format (for example "submitted 2 days ago") [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I calculate relative time?
I want to format dates on my social web app much like Digg.com and other sites do. There, you see very friendly dates, such as:
just now
3 minutes ago
one hour ago
2 weeks ago
6 months ago
etc
Before I wrap my head around creating such a thing, does anyone know of any ready-to-go script for this where I simply insert a datestamp and a friendly date text is given based on how it related to the current time?
PS: I need this in PHP, but pseudo-code or any other language is fine too.
This is a duplicate of this question. It has a flurry of code samples on how to accomplish this, in addition to the code this very site uses. I glanced at it and there seems to be a PHP implementation posted there too.
In addition to all this, if are you using jQuery you can do this client-side with something like the timeago plugin. It has the advantage of updating the text as time passes so if you load a page and it says "posted 5 minutes ago" and look again 5 minutes later, it says "posted 10 minutes ago"
Thanks all for the answers, and sorry for the duplicate question. I did not find the duplicate when I was looking for it because I did not really know what search terms to use.
Anyways, I have my problem solved thanks to the PHP translation of the code used by stackoverflow. I made one tiny change in calculating the delta:
$delta = strtotime(gmdate("Y-m-d H:i:s", time())) - $time;
Since I am storing my dates in MySQL as timestamp in the GMT format, I have to use the same for calculating the CURRENT time. This makes for a timezone neutral comparison, which is exactly what is needed in my case.
You can also do this in SQL:
Best way to convert DateTime to "n Hours Ago" in SQL

Categories