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.
Related
We've got a collection of messy data, and trying to unify it.
Lots of services let you type dates out into different formats and they correctly understand them, but cant think what the process is called, or how we could go about doing this in PHP, if there is a library that already provides this.
So we've got time and dates in an old database we've inherited, and trying to clean it up a bit, some of the formats look like
9pm
9:00pm
25th march 2015
its a complete mix and match, does anybody know of any libraries or ways to be able to parse these into a universal format?
the problem here is the information you have is inconsistent! you need to normalize it some way, IT might actually be worth getting into an excel sheet and try to match the date time fields into some kind of regex and filter like that, is probably what i would do, so you can separate the different formats and tackle each format individually.
A program will have to first identify the format you're feeding it and then it will spit out whatever format you want!
you can use this strtotime() PHP with this to turn it into any format
http://php.net/manual/en/function.date.php
I am trying to parse 3 RSS feeds for titles, links, and dates for every item. While the xml for titles and links are the same across the RSS (, ), the dates (including the time) are formatted differently across the 3, but in the interest of creating a Php object where I aggregate all the dates to display on a map, they need to be formatted similarly.
I'm currently trying to use the Php function (strtotime - http://php.net/manual/en/function.strtotime.php) to create a UNIX timestamp. Does anyone know if this is the right way to do it?
The 3 date formats I currently have are:
Wed, 25 Nov 2015 17:20:18 +0000
03 Dec 2015 21:30:00 GMT
2015-12-10T05:00:00Z
Thanks!
For this type of solutions do you have the design pattern Chain-Of-Responsibility, see for instance
https://en.wikipedia.org/wiki/Chain-of-responsibility_pattern.
Every part of the chain knows how to deal with a specific format. It will check if the data is consistent with the format or not. If not, then will it pass the data to the next class in the chain. If it succeeds, then will the process stop and you will have your result.
If you need more formats in the future, then will you be able to extend the chain with another implementation without changing that what already works as designed.
An uml of the design pattern can be found here:
http://www.oodesign.com/chain-of-responsibility-pattern.html.
I am trying to add a Google calendar to a website I'm making that the client will be able to update themselves. I found this: http://mikeclaffey.com/google-calendar-into-html/ which has been quite helpful, but I am a bit stuck.
The website I am building is using PHP template and the page contents is contained within the $content variable. Here is the link I'm working on: http://victoriasawyer.com/AmosTheTransparent.
The calendar feed is the top one of the two lists of Tour Dates. I would like the top one to look similar to the bottom one. The same would be ideal but not necessary.
The biggest issue I'm having is with the start date and time. In the title of the event I would like just the date to show (preferably like 10/03/14) not the time. I have figured out how to display just the time separately without issue. Is there some way to change the date format?
The other issue I'm having is the order that the events are appearing. I would like the events to show with the soonest one first in the list and the furthest one last in the list. I added the additional parameters as instructed in the tutorial (orderby=starttime and sortorder=ascending) but they do not display correctly.
The url I am using is: https://www.google.com/calendar/feeds/qmfadhgtq2kmabsi3dlb456v98%40group.calendar.google.com/public/full?orderby=starttime&sortorder=ascending&futureevents=true&alt=json. Is there something I can adjust or add to fix the order?
Any help will be much appreciated (even if you can recommend an alternative. It just needs to be customizable, and so far this seems to be the best option I have found).
You can use PHP's DateTime class like this:
$date = new DateTime('Sun Nov 16 2014 00:30:00 GMT+0000');
echo $date->format('Y-m-d');
You can then use the predefined formats in PHP which are listed here to format it into whatever style you like.
echo $date->format('d/m');
echo $date->format('d/m/Y');
What I'm trying to do, is to fill in a hidden input in a login/register page for a website, and inside that input will contain the JavaScript new Date() thing, like:
"Fri Mar 23 2012 22:23:03 GMT-0700 (PDT)"
I would like to take that information and use it server-side to convert it to the PHP date() and display it in the user's timezone.
It seems like there would be an easy way using the GMT difference of -0700, but I've looked all over and I can't find a way to simply plug that number in.
Is there an easy solution to this? All I want to do is modify the output to the users time after submitting the form.
Use date_create_from_format() or a similar function. Make sure to output the date from Javascript in the same format you intend to read.
Also see this old answer from ... myself:
How to show server and user time in 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