开发者

PHP MySQL display todays date as Today

开发者 https://www.devze.com 2023-03-14 20:29 出处:网络
I notice on forums that when a topic is today\'s date, the date is dis开发者_高级运维played as Today. How do you do that?pseudo code:

I notice on forums that when a topic is today's date, the date is dis开发者_高级运维played as Today. How do you do that?


pseudo code:

if date == today then display "Today" else display date


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);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消