i am unsure about how to begin this..
i want to make a function to check if a given date is past midnight. if it is, its "today". if its before then, check to see if its within the past 24 hours midnight. if it is, its yesterday. and 1 more, then 2 days ago.
i need that to go on until "3 days ago".
i am unsure about how to go about starting this, can someone help?
While @Mwnciau's code may work I was thinking about using the mktime() function. I don't have any code to offer up but my thinking is along these lines:
1. Get todays date and hour and convert into the unix timestamp via mktime(). Now you have a baseline to work from.
2. Take what ever date you wish to check and convert that into a unix timestamp also. Now all you hvae to do is subtract the difference and figure out how far back that date is.
There are 86400 seconds in the unix timestamp per 24 hour period - so to subtract one day you would simply subtract 86400 from your timestamp value. The hourly value is 3600. So if you did your base line at 6:00am today then 3600 * 6 == 21600 seconds. Let's say your baseline timestamp for today (at 6am) was 1184195100 so subtracting 3600 from that value would give you midnite. I think you can see how that would work.
On the other hand, maybe we could just go with @Mwnciau's code
Get todays date and hour and convert into the unix timestamp via mktime(). Now you have a baseline to work from.
Or just use the result of time()?
Don't use seconds though. If it's 11 AM, and you go back 12 hours, the difference would be less than 86400 seconds using your idea, so it would show as today. But 11 PM last night is considered yesterday. It's better to use the day.
PHP Code:
function timeDiffToString($time) {
$dayToday = date('z');
$dayTime = date('z', $time);
if ($dayToday == $dayTime) {
return "Today";
} else if ($dayToday - 1 = $dayTime) {
return "Yesterday";
} else {
// return whatever you want as default
}
}
You'd still need to check year, etc. But that's the basic idea.