I’m pulling in some tweets and I’d love to output whether or not they came in today, yesterday, or before that. Here’s the code that I’m working with (which doesn’t work). It’s skipping the if and else if and going straight to the else.
date_default_timezone_set("America/New_York");
$time = $block["created_at"];
$time = strtotime($time);
if(date("now") == date("m-d-Y", $time)) {
$time = date("g:ia", $time);
}
else if(date(strtotime("-1 day")) == date("m-d-Y", $time)) {
$time = "Yesterday at" + date("g:ia", $time);
}
else {
$time = date("m-d-Y g:ia", $time);
}
Absolutely ideally, I’d also love it if, when the timestamp falls within the last 6 days, it shows the day of the week and the time, and anything older than that will show the date.
So the stream might look like this (dates sorted descending):
[…] 9:53am
[…] 7:02am
[…] Yesterday at 11:24pm
[…] Monday at 3:45pm
[…] Jan 2, 2013
Any idea as to where my code is going awry? Thanks!
The
strtotimefunction can parse various strings:All you need to do now is to compare the given timestamp with the above in descending order:
Note that
strtotime()returns a timestamp (an integer) whiledate()returns a string. The function compares timestamps with timestamp.