dede from_unixtime函数如何正确使用?

99ANYc3cd6
预计阅读时长 19 分钟
位置: 首页 DEDE建站 正文

Of course! Let's break down the dede:from_unixtime tag in DedeCMS (织梦内容管理系统).

dede from_unixtime
(图片来源网络,侵删)

What is dede:from_unixtime?

In simple terms, dede:from_unixtime is a DedeCMS template tag used to convert a Unix timestamp into a human-readable date and time format.

Why is this needed?

  • Unix Timestamp: This is a number representing seconds since January 1, 1970 (UTC). It's how databases (like MySQL) and many programming systems store dates and times because it's efficient for sorting and calculations.
  • Human-Readable Format: Website visitors don't see timestamps like 1678886400. They want to see March 15, 2025 or 2025-03-15 10:30:00.

The dede:from_unixtime tag acts as a translator, making the date stored in your database easy for users to understand.


Syntax and Parameters

The basic syntax of the tag is:

{dede:from_unixtime timestamp='你的时间戳' format='日期格式' /}

It has two main parameters:

dede from_unixtime
(图片来源网络,侵删)

timestamp (Required)

This is the Unix timestamp you want to convert. It's not always a static number. It's most commonly used to display the publication time of an article.

  • Common value: [field:pubdate /] or [field:senddate /]
    • [field:pubdate /]: The article's publication date (from the pubdate field in the dede_archives table).
    • [field:senddate /]: The article's submission date (from the senddate field).

Example:

{dede:from_unixtime timestamp='[field:pubdate /]' /}

format (Optional)

This parameter controls the output format of the date and time. If you don't provide it, DedeCMS uses a default format (usually Y-m-d H:i:s).

Here are the most common format codes you can use:

dede from_unixtime
(图片来源网络,侵删)
Code Description Example Output
Y Four-digit year 2025
y Two-digit year 23
m Two-digit month (with leading zero) 03
n Month without leading zero 3
d Two-digit day of the month 15
j Day of the month without leading zero 15
H 24-hour format (with leading zero) 14
h 12-hour format (with leading zero) 02
i Minutes (with leading zero) 05
s Seconds (with leading zero) 09
F Full month name March
M Three-letter month name Mar
D Three-letter day name Tue
l Full day name Tuesday

Practical Examples

Let's assume an article's [field:pubdate /] is 1678886400, which corresponds to 2025-03-15 10:30:00.

Example 1: Default Format

If you don't specify a format, it will look like this:

{dede:from_unixtime timestamp='[field:pubdate /]' /}

Output:

2025-03-15 10:30:00

Example 2: Standard Date Format (e.g., for a list)

This is very common for displaying just the date of an article.

{dede:from_unixtime timestamp='[field:pubdate /]' format='Y-m-d' /}

Output:

2025-03-15

Example 3: More Readable Format (e.g., "March 15, 2025")

This is great for improving user experience.

{dede:from_unixtime timestamp='[field:pubdate /]' format='F j, Y' /}

Output:

March 15, 2025

Example 4: Including Time in 12-hour Format

Use this if you want to show AM/PM.

{dede:from_unixtime timestamp='[field:pubdate /]' format='Y-m-d h:i:s A' /}

Output:

2025-03-15 10:30:00 AM

Example 5: Displaying the Update Time

You can use it for other timestamps in the system, like the article's update time (uptime).

更新时间: {dede:from_unixtime timestamp='[field:uptime /]' format='Y-m-d H:i' /}

Output:

更新时间: 2025-03-16 16:45

Common Use Cases in Templates

  1. In article_article.htm (Article Detail Page): Display the article's publication date in a user-friendly way.

    <div class="article-info">
        <span>发布时间:{dede:from_unixtime timestamp='[field:pubdate /]' format='Y-m-d' /}</span>
        <span>作者:{dede:field name='writer' /}</span>
        <span>来源:{dede:field name='source' /}</span>
        <span>点击:{dede:field name='click' /}</span>
    </div>
  2. In list_artlist.htm (Article List Template): Show the date for each article in a list.

    {dede:list}
    <li>
        <a href="[field:arcurl/]">[field:title/]</a>
        <span class="date">{dede:from_unixtime timestamp='[field:pubdate /]' format='Y-m-d' /}</span>
    </li>
    {/dede:list}

Troubleshooting

  • Problem: The tag shows nothing or a strange error.

    • Check 1: Make sure the timestamp parameter is correctly set, usually to [field:pubdate /]. Double-check that this field has a value in your database.
    • Check 2: Ensure you are using the correct DedeCMS tag syntax. The closing is important: {dede:from_unixtime ... /}.
  • Problem: The date format is wrong.

    • Check: Review your format string. Remember that characters like , , , and spaces are treated as literal characters. Use the format codes from the table above correctly. For example, Y-m-d is correct, but Y m d would produce 2025 03 15.
-- 展开阅读全文 --
头像
织梦tag标签静态插件5.7下载安全吗?
« 上一篇 12-12
C语言中printf的%d是什么意思?
下一篇 » 12-12

相关文章

取消
微信二维码
支付宝二维码

目录[+]