PHP:时间 - “今天” - “昨天”按日期而不是小时

时间:2013-08-23 22:02:28

标签: php sql time

我刚想到这个:

$dates = date(" d-m-y",$forum_tid);
if ($dates == date(' d-m-y')) {
    $day_name = 'This day';
} else if($dates === date(" d-m-y", strtotime("-1 day"))) {
   $day_name = 'Yesterday';
} else {
    $day_name = 'Another day';
}
echo "$day_name";

现在运行24小时时钟系统。现在我希望它按日期工作。 (如果时间是同一天,则说“今天”,如果是f.ex 23-08-13之前的日期,那么它显示“昨天”)

我该怎么做?希望这是一个更让人疑惑的问题!

2 个答案:

答案 0 :(得分:1)

使用DateTime类:

$date = new DateTime();
$date->setTimestamp($forum_tid);

$today = new DateTime();
$yesterday = new DateTime('-1day');

switch(TRUE) {

    case $today->format('m-d') === $date->format('m-d'):
        $day_name = 'This day';
    break:

    case $yesterday->format('m-d') === $date->format('m-d'):
        $day_name = 'Yesterday';
    break;

    default:
        $day_name = 'Another day';

}

echo "$day_name";

答案 1 :(得分:-1)

不要使用===进行字符串比较。

你可以在12点开始

strtotime(date("Y-m-d"));

所以你可以这样做:

if(strtotime($mydate) > strtotime(date("Y-m-d"))) {
    echo "Today";
} elseif(strtotime($mydate) > strtotime(date("Y-m-d", strtotime("-1 day")))) {
    echo "Yesterday";
} elseif(strtotime($mydate) > strtotime(date("Y-m-d", strtotime("-1 week")))) {
    echo "In the last week";
} else {
    echo "Older";
}