In website content management, the way time is presented is crucial to user experience.Although the system may prefer a unified and efficient Unix timestamp format when processing data in the background, a string of random numbers is obviously less intuitive and understandable than 'October 27, 2023 14:35'.Auto CMS knows this and provides simple yet powerful tools to solve this problem.

Unix timestamp: 'time language' in the database

Unix timestamp, in short, is the number of seconds elapsed since midnight on January 1, 1970 (UTC/GMT).It is a globally unified time representation that does not change with regions and time zones, which is very suitable for database storage, data transmission, and programming calculation.CreatedTime)Update time(UpdatedTime),even the login records or VIP expiration time of users, are usually stored in this digital form.

However, when these timestamps are directly presented on the website front end, they will only be a series of numbers, for example1678886400. Such display is meaningless for ordinary users, from which no date or time information can be obtained.

Security CMS solution:stampToDatetags

To make these numbers 'speak', Anqi CMS provides a feature namedstampToDateThe template tag, which can easily convert Unix timestamps to any readable date and time format you need.

The basic usage of this tag is very intuitive:{{stampToDate(时间戳, "格式")}}

Here:

  • 时间戳is the Unix timestamp you want to convert, which is usually 10 digits. In the template of AnQi CMS, this will usually beitem.CreatedTime/archive.UpdatedTimeetc. variables.
  • 格式This is a string that defines how you want the date and time to be displayed.

Understand the time formatting rules of the Go language

stampToDateThe core of the label lies in its second parameter—the date-time format string. Anqi CMS is developed based on the Go language, so its time formatting follows the rules unique to the Go language instead of the commonY-m-d H:i:soryyyy-MM-dd HH:mm:ss.

Go language uses a fixed reference time2006-01-02 15:04:05As a formatted template. You just need to replace the year, month, day, hour, minute, second, and other elements in this reference time with the display style you want.

For example:

  • If you want to displayYear-Month-DayThen the format string is"2006-01-02".
  • If you want to displayYear/Month/DayThen the format string is"2006/01/02".
  • If you want to displayYear Month Day (Chinese)Then the format string is"2006年01月02日".
  • If you want to displayHour:Minute:SecondThen the format string is"15:04:05".
  • If you want to displayYear-Month-Day Time: minuteThen the format string is"2006-01-02 15:04".

You can also combine these elements, even add other characters, such as days of the week or AM/PM, and the Go language will intelligently parse and output according to the template you define.

Actual application example: Formatting document publish time in templates

The article list page you are building, and you want to display the publication time of the article in the format of “Year-Month-Day Hour:Minute”.archiveListorarchiveDetailLabel.

An English translation would be: A common application scenario is to display the publication time of articles:.

{# 在一个文章列表循环中 #}
{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
    <li>
        <a href="{{item.Link}}">
            <h5>{{item.Title}}</h5>
            <div>
                <span>发布于:{{stampToDate(item.CreatedTime, "2006年01月02日 15:04")}}</span>
                <span>更新于:{{stampToDate(item.UpdatedTime, "2006-01-02 15:04:05")}}</span>
            </div>
        </a>
    </li>
    {% endfor %}
{% endarchiveList %}

In the above code,item.CreatedTimeanditem.UpdatedTimeThat is, the Unix timestamp retrieved from the database. ThroughstampToDateTags, they are converted to user-friendly date and time strings. For example, ifitem.CreatedTimeYes1678886400, it may be displayed as “2023年03月15日 00:00” after formatting.

You can also format the current document's time directly on the document details page:

{# 在文档详情页中 #}
<div>
    <h1>{% archiveDetail with name="Title" %}</h1>
    <p>发布时间:{% archiveDetail archiveCreatedTime with name="CreatedTime" %}{{stampToDate(archiveCreatedTime, "2006年1月2日 下午3点04分")}}</p>
    <p>最近更新:{% archiveDetail archiveUpdatedTime with name="UpdatedTime" %}{{stampToDate(archiveUpdatedTime, "2006-01-02 15:04:05")}}</p>
</div>

Here are two different custom formats displayed, you can choose flexibly according to the page design and user habits.

More formatting scenarios

In addition to the article's publication and update time, there are many other scenarios in the Anqi CMS that involve timestamps, such as the user's registration time, the last login time, and the expiration time of VIP members. As long as you can obtain these timestamp variables, you can usestampToDateLabel formatting.

For example, display the user's last login time:

{# 假设 userDetail 标签可以获取到用户详情 #}
{% userDetail lastLogin with name="LastLogin" id="1" %}
<p>最后登录时间:{{stampToDate(lastLogin, "2006/01/02 15:04")}}</p>

Summary

PassstampToDate


Common Questions (FAQ)

1. The Unix timestamp I provide is 13 digits,stampToDateCan the tag handle it?CurrentlystampToDateThe tag is designed to handle standard 10-digit Unix timestamp (second-level).If your timestamp is 13 digits (millisecond level), you need to divide it by 1000 before passing it in to convert it to a 10-digit second-level timestamp.{{stampToDate(item.CreatedTime / 1000, "2006-01-02")}}.

2. Is there any other format for date and time tags or filters in AnQi CMS?AnQi CMS template engine also supports a format nameddateThe filter, but it requires the value to be already in Go language'stime.Timetype, not the original Unix timestamp. For Unix timestamps directly retrieved from the database,stampToDateLabel is a more direct and recommended formatting method.dateFilter is more suitable for the ones that have been processed by Go backend and converted totime.Timevariable of the object.

3. Why does Go language use time formatting2006-01-02 15:04:05Is this number a reference? What special meaning does it have?This is a unique feature of the design of the Go language, which does not use placeholders like other languagesYRepresents the year,m代表月份),而是选择了一个具体的日期和时间作为格式化模板。这个日期2006年1月2日 下午3点4分5秒just a "reference point" used in the official Go language documentation to display all time elements. Each number and position represents the corresponding date and time component. For example,2006Represents a four-digit year,01Represents a two-digit month,02Represents a two-digit date,15Represents the hour number in 24-hour format, etc. Just remember this special date-time string and replace the numbers as needed to define the output format.