档案生成器 PHP

发布于 2024-07-29 00:24:46 字数 691 浏览 7 评论 0原文

在我开始之前,我想说我发布这个问题更多的是作为讨论而不是问题问题。

在我的数据库中,我有新闻帖子,比如说有 3 列(ID、标题、日期)。 当 Id 和 title 是 self Explanitory 时,日期存储在 mktime() 值中,换句话说,自 1970 年 1 月 1 日以来经过的秒数。

现在我想做的是构建一个存档链接,该链接将显示为

  • July 2009
  • June 2009
  • 2009 年 3
  • 月 2009 年 2
  • 月 2008 年 12 月

请注意,没有显示帖子的月份。

现在,我最初的想法是

  1. 从当月的最后一天开始
  2. ,并获取第一个的值对日期
  3. >= First_Day_Seconds AND 日期 <= Last_Day_Seconds 的帖子执行 MySQL COUNT 查询/mysql_num_rows 将
  4. 值显示或放入数组中
  5. 执行另一个查询以检查是否找到任何更多值 WHERE date < First_Day_Seconds(如果没有找到行则中断)

现在上面的内容只是我的想法。 但如果您有任何加快此过程的想法,请分享。

提前会说,日期需要是mktime格式

Before i start id like to say ive posted this question as more of a discussion rather than Problem Question.

In my Database i have news posts lets say with 3 columns (Id, title, date). Wher Id and title are self Explanitory the date is stored in mktime() values, in other words the number of seconds passed since 1 January 1970.

Now what i want to do is build an archive link that will display as such

  • July 2009
  • June 2009
  • March 2009
  • Feburary 2009
  • December 2008

Note the months on which there were no posts are not displayed.

Now as an initial thought i was thinking

  1. Start with the last day of the current Month
  2. And get the Value of the First day of the current Month
  3. Do a MySQL COUNT Query/mysql_num_rows for posts that were date >= First_Day_Seconds AND date <= Last_Day_Seconds
  4. Display or put the values in an Array
  5. Do another Query to Check if Any more values are found WHERE date < First_Day_Seconds (break if no rows were found)

Now the above is just something on the top of my head. But if you got any ideas to speed this process up please share.

Will say in advance, date needs to be in mktime format

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

慵挽 2024-08-05 00:24:46

我建议使用数据库“本机”时间格式,但它也适用于 UNIX 时间戳。

您可以简单地执行以下操作:

SELECT DISTINCT FROM_UNIXTIME(date, '%M %Y') FROM posts;

(可选)使用 WHERE 子句将日期限制为过去或未来的日期。 可能会添加一个 ORDER 子句以达到良好的效果。 这应该就是所需要的全部了,让数据库做尽可能多的工作。

如果您需要更多格式选项,请选择带有 "%Y-%m" 的日期,然后用 PHP 格式化它们:

date($myCustomFormat, strtotime("$date-01"));

I would suggest using a database "native" time format, but it works with UNIX timestamps as well.

You can simply do:

SELECT DISTINCT FROM_UNIXTIME(date, '%M %Y') FROM posts;

Optionally with a WHERE clause limiting the dates to past or future dates. Possibly an ORDER clause thrown in for good measure. That should be pretty much all that's needed, let the database do as much work as possible.

If you need more formatting options, select the dates with "%Y-%m" instead and format them in PHP:

date($myCustomFormat, strtotime("$date-01"));
岁月静好 2024-08-05 00:24:46

您可以使用此查询来获取年份

"SELECT *,content_id,COUNT(content_id) AS itemCount FROM content_mast GROUP BY DATE_FORMAT(date_upload,'%Y') DESC";

现在可以使用此查询来获取当年的月份

$tday = date("Y", $datetime);
$s1="select * from content_mast where DATE_FORMAT(date_upload,'%Y')=$tday";

You can use this query to get years

"SELECT *,content_id,COUNT(content_id) AS itemCount FROM content_mast GROUP BY DATE_FORMAT(date_upload,'%Y') DESC";

now use can use this query to get month of that year

$tday = date("Y", $datetime);
$s1="select * from content_mast where DATE_FORMAT(date_upload,'%Y')=$tday";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文