我查看了 PHP 日期手册,但找不到任何有关它的文档。我在荷兰网站上找到的http://marktplaats.nl 。如果你看this url你可以看到“Vandaag”这意味着“今天”,他们是怎么做到的?
我现在拥有的代码:
<?= date ("d M 'y",strtotime($row['aangeboden-sinds'])); ?>
最佳答案
这很有可能是在 SQL 中完成的,而不是在应用程序代码中。以下是如何在 PostgreSQL 中执行此操作。 (该查询在 MySQL 5.6 中似乎无需修改即可运行,这让我有点惊讶。)
create table test (
test_id integer primary key,
created_at timestamp not null default current_timestamp
);
insert into test values
(1, current_timestamp),
(2, current_timestamp - interval '1' day),
(3, current_timestamp - interval '10' day);
使用 CASE 语句和简单的日期算术几乎可以完成您想要的任何操作。
select test_id,
case current_date - cast(created_at as date)
when 0 then 'Added today'
when 1 then 'Added yesterday'
else 'Added some time ago'
end as when_added
from test
order by created_at desc, test_id asc
TEST_ID WHEN_ADDED
--
1 Added today
2 Added yesterday
3 Added some time ago
当您按created_at desc排序
时,您的数据自然会按显示顺序返回(不考虑您的应用程序可能需要的任何其他列),并且在php中您只需要显示“when_added”列。您不需要在 php 中进行任何操作。
关于php - 如何将 'Added Today' 或 'Added yesterday' 添加到 mysql 新添加的记录中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17811807/