dede getdatetimemk函数如何使用?

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

Of course. The GetDateTimeMK function is a very specific and useful function in the legacy DedeCMS (织梦CMS) templating engine. It's used to format date and time information in a particular, localized way that was common in Chinese web design.

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

Let's break it down in detail.


What is GetDateTimeMK?

GetDateTimeMK is a DedeCMS custom function (PHP function) designed to be used within DedeCMS templates (*.htm files). Its primary purpose is to take a timestamp and format it into a Chinese-style date and string, which was very popular in the mid-2000s.

It's essentially a specialized formatting tool for dates.

Syntax

The syntax is simple and straightforward:

dede getdatetimemk
(图片来源网络,侵删)
{dede:field function='GetDateTimeMK(@me)'}

Parameters

  • @me: This is a special DedeCMS template variable. When used inside a {dede:field} tag, @me refers to the current value of that field. For GetDateTimeMK, @me is expected to be a Unix timestamp.

How It Works: The Core Logic

The GetDateTimeMK function takes a Unix timestamp and converts it into a string formatted as YYYY年MM月DD日 HH:mm. It's a shortcut for creating a localized, human-readable date.

Let's look at the PHP source code of the function to understand exactly what it does:

// This is the typical PHP code for the function in DedeCMS
function GetDateTimeMK($arcid)
{
    // Create a new DateTime object from the timestamp
    $dtime = new Date("@$arcid");
    // Format the date into the Chinese style
    $time = $dtime->format('Y年m月d日 H:i');
    return $time;
}

Explanation:

  1. It receives a timestamp (e.g., 1625097600).
  2. It uses PHP's Date class (a DedeCMS wrapper for DateTime) to create an object from that timestamp.
  3. It formats the date using the specific format string: 'Y年m月d日 H:i'.
    • Y: 4-digit year (e.g., 2025)
    • m: 2-digit month (e.g., 06)
    • d: 2-digit day (e.g., 25)
    • H: 24-hour format hour (e.g., 14)
    • i: Minutes with leading zeros (e.g., 30)
  4. The characters , , , and are literal separators.
  5. It returns the final string, for example: 2025年06月25日 14:30.

Practical Usage Examples

Here are the most common places you'll use GetDateTimeMK in a DedeCMS template.

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

Example 1: Displaying an Article's Publication Date

This is the most frequent use case. In an article list (arclist.htm) or article detail (article_article.htm) template, you want to show when the article was published.

In arclist.htm (Article List):

<ul>
    {dede:arclist titlelen='30' row='10'}
    <li>
        <a href="[field:arcurl/]">[field:title/]</a>
        <span class="date">发布于: {dede:field function='GetDateTimeMK(@me)'/}</span>
    </li>
    {/dede:arclist}
</ul>

Note: In arclist, the pubdate field is already a timestamp, so @me works perfectly.

In article_article.htm (Article Detail Page):

<div class="article-info">
    <span>发布时间:{dede:field.pubdate function='GetDateTimeMK(@me)'/}</span>
    <span>作者:{dede:field.writer/}</span>
    <span>来源:{dede:field.source/}</span>
</div>

Example 2: Displaying a Comment's Post Time

Similarly, for comments, you can use it to format the dtime field.

In templets/feedback.htm (Comment List):

{dede:feedback}
<div class="comment-item">
    <div class="comment-header">
        <strong>[field:username function='htmlspecialchars(@me)'/]</strong> 说:
        <span class="comment-time">{dede:field.dtime function='GetDateTimeMK(@me)'/}</span>
    </div>
    <p>[field:msg function='htmlspecialchars(@me)'/]</p>
</div>
{/dede:feedback}

GetDateTimeMK vs. MyDate

It's crucial to understand the difference between GetDateTimeMK and the more general MyDate function, as they are often confused.

Feature GetDateTimeMK MyDate
Purpose Specialized: Formats a date into a single, specific Chinese style. General Purpose: Formats a date into any custom format you specify.
Syntax {dede:field function='GetDateTimeMK(@me)'} {dede:mydate('format-string', @me)}
Flexibility Low. Always outputs YYYY年MM月DD日 HH:mm. High. You control the output format.
Use Case When you specifically want the 年月日 format. When you need any other format, e.g., Y-m-d, Y/m/d, etc.

Example with MyDate:

If you wanted to display the date as 2025-06-25, you would use MyDate:

<!-- Using MyDate to get a different format -->
{dede:field.pubdate function='MyDate("Y-m-d", @me)'}
<!-- This would output: 2025-06-25 -->

Modern Considerations and Alternatives

  1. Legacy System: GetDateTimeMK is a hallmark of older DedeCMS versions (from DedeCMS 5.x era). While it still works in modern versions, the trend is to move away from these custom PHP-in-template functions for better security and cleaner code.

  2. Security: The function='...' syntax can be a security risk if not properly sanitized, as it allows arbitrary PHP function execution. Modern DedeCMS versions have tightened this, but it's a good practice to be aware of.

  3. Modern PHP (PHP 8+): If you are modifying PHP files directly (e.g., include/taglib/arclist.lib.php), it's better to use modern PHP date/time functions.

    Old Way (in PHP):

    $time = GetDateTimeMK($row['pubdate']);

    Modern PHP Way:

    // Use date_create() and format()
    $date = date_create('@' . $row['pubdate']);
    $time = date_format($date, 'Y年m月d日 H:i');
    // Or simply, if you are on PHP 8.0+
    $time = date('Y年m月d日 H:i', $row['pubdate']);

Summary

  • What it is: A DedeCMS template function to format a Unix timestamp into YYYY年MM月DD日 HH:mm.
  • How to use: {dede:field function='GetDateTimeMK(@me)'}.
  • Best for: Quickly displaying a localized Chinese date in articles, comments, etc.
  • Not for: When you need a custom date format; use {dede:mydate} instead.
  • Status: A legacy but still functional part of DedeCMS.
-- 展开阅读全文 --
头像
dede getoptionlist如何调用与使用?
« 上一篇 2025-12-13
织梦.htaccess伪静态规则如何正确配置?
下一篇 » 2025-12-13

相关文章

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

目录[+]