PHP MySQL显示今天的日期为今天

时间:2011-06-23 20:40:22

标签: php mysql date

我在论坛上注意到,当主题是今天的日期时,日期显示为今天。你是怎么做到的?

2 个答案:

答案 0 :(得分:4)

伪代码:

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

答案 1 :(得分:3)

有很多方法可以做。从纯粹的MySQL角度来看:

SELECT 
    IF(CAST(my_timestamp_column AS DATE) = CURRENT_DATE, 1, 0) AS is_today 
FROM ...

在PHP中,

if (date('Y-m-d', $timestamp) == date('Y-m-d')) { ...

再次,有很多方法可以做到这一点

编辑:更多MySQL:

SELECT
    IF(
        CAST(ts AS DATE) = CURRENT_DATE,
        CONCAT('Today at ', CAST(ts AS TIME)),
        ts
    ) AS fancy_date

或更多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);
}