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!
You can pass the value you get from Date. now() to new Date() this will give you the time in readable format.
Getting the Current Time Stamp const currentDate = new Date(); const timestamp = currentDate. getTime(); In JavaScript, a time stamp is the number of milliseconds that have passed since January 1, 1970.
Your code should look liike below.
if(date("m-d-Y") == date("m-d-Y", $time)) {
$time = date("g:ia", $time);
}
else if(date("m-d-Y", strtotime("-1 day")) == date("m-d-Y", $time)) {
$time = "Yesterday at" + date("g:ia", $time);
}
else {
$time = date("m-d-Y g:ia", $time);
}
Explanation
date() — Format a local time/date. Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given. In other words, timestamp is optional and defaults to the value of time()
.
Where have you gone wrong ?
You should 1st pass the format then the timestamp to the date()
function. Read more.
The strtotime
function can parse various strings:
echo date("D Y-m-d H:i:s"); // Wed 2013-01-09 21:33:07
echo date("D Y-m-d H:i:s", strtotime("today 00:00" )); // Wed 2013-01-09 00:00:00
echo date("D Y-m-d H:i:s", strtotime("yesterday 00:00")); // Tue 2013-01-08 00:00:00
echo date("D Y-m-d H:i:s", strtotime("-6 day 00:00" )); // Thu 2013-01-03 00:00:00
echo date("D Y-m-d H:i:s", strtotime("-100 day 00:00" )); // Mon 2012-10-01 00:00:00
All you need to do now is to compare the given timestamp with the above in descending order:
function formatDate($time) {
if ($time >= strtotime("today 00:00")) {
return date("g:i A", $time);
} elseif ($time >= strtotime("yesterday 00:00")) {
return "Yesterday at " . date("g:i A", $time);
} elseif ($time >= strtotime("-6 day 00:00")) {
return date("l \\a\\t g:i A", $time);
} else {
return date("M j, Y", $time);
}
}
echo formatDate(time()); // 9:42 PM
echo formatDate(strtotime("-1 day")); // Yesterday at 9:42 PM
echo formatDate(strtotime("-6 day")); // Thursday at 9:42 PM
echo formatDate(strtotime("-100 day")); // Oct 10, 2012
Note that strtotime()
returns a timestamp (an integer) while date()
returns a string. The function compares timestamps with timestamp.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With