I notice on forums that when a topic is today's date, the date is displayed as Today. How do you do that?
Asked
Active
Viewed 720 times
-1
-
possible duplicate of [PHP How to find the time elapsed since a date time?](http://stackoverflow.com/questions/2915864/php-how-to-find-the-time-elapsed-since-a-date-time) – BrunoLM Jun 23 '11 at 23:44
2 Answers
4
pseudo code:
if date == today then display "Today" else display date

Abe Miessler
- 82,532
- 99
- 305
- 486
3
There are many ways that could be done. From a purely MySQL standpoint:
SELECT
IF(CAST(my_timestamp_column AS DATE) = CURRENT_DATE, 1, 0) AS is_today
FROM ...
in PHP,
if (date('Y-m-d', $timestamp) == date('Y-m-d')) { ...
Again, tons of ways to do this
EDIT: More MySQL:
SELECT
IF(
CAST(ts AS DATE) = CURRENT_DATE,
CONCAT('Today at ', CAST(ts AS TIME)),
ts
) AS fancy_date
or some more php:
$ts = strtotime($mysql_query_result['timestamp_column']);
$todayStart = mktime(0,0,0);
$todayEnd = mktime(23,59,59);
if ($ts <= $todayEnd && $ts >= $todayStart) {
echo 'Today at ', date('H:i:s', $ts);
}
else {
echo date('Y-m-d H:i:s', $ts);
}

Community
- 1
- 1

Dereleased
- 9,939
- 3
- 35
- 51