【发布时间】:2013-01-24 20:51:22
【问题描述】:
我已经搜索过这个,但我找不到任何东西。
我有一个文章表。当我写新文章时,我使用 php time() 函数将时间写入 mysql 表。
我的桌子是
id文章时间
时间数据类型为 INT(UNSIGNED)
我的问题是我想显示上周到今天或上个月到今天的文章。
我的 mysql 查询应该如何?
通常我会这样做
SELECT * FROM articles ORDER BY Time DESC
所以这给了我从今天到过去的数据。我该怎么做?我想不出解决办法。我应该一起使用 php-mysql 还是只能用 mysql 来处理?你能给我想法或例子吗?谢谢。
编辑:
我按照建议更改为 datetime,现在我认为我遇到了时区问题
现在我的 ago() 函数工作晚了 2 小时。
<?php
date_default_timezone_set('Europe/Istanbul'); //timezone function
ago($time)
{
$periods = array("saniye", "dakka", "saat", "gün", "hafta", "ay", "yıl", "baya");
$lengths = array("60","60","24","7","4.35","12","10");
$now = time();
$difference = $now - $time;
$tense = "önce";
for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
$difference /= $lengths[$j];
}
$difference = round($difference);
return "$difference $periods[$j] önce ";
} //ago function
echo $Date = ago(strtotime($data['WriteTime'])). 'önce';
?>
【问题讨论】:
-
time是int?它至少是一个 Unix 标记吗?将表更改为使用time数据类型。 -
正如@njk 已经建议的那样:使用
Date- 或DateTime-列,然后让MySQL 做一切(意思是:不要使用time()at all:不需要)。