In website operation and management, user behavior data is an important basis for improving user experience and formulating strategies. Among them, the "last login time" (LastLoginIt is a direct indicator of user activity. However, this data is usually stored in the form of raw timestamps, which are not as friendly and easy to understand for ordinary users as a direct date and time.AnQi CMS provides powerful template tag features, among whichstampToDateLabels are the tools to solve this problem, they can help us easily convert abstract timestamps into clear and readable date formats.

UnderstandingLastLoginand its importance

In Anqi CMS template development, you may encounter in user detail pages, member centers, or modules that need to display user activity informationLastLoginThis field records the system time of the user's last successful login. It is usually a 10-digit or 13-digit integer (Unix timestamp). For example,1678886400Such numbers are precise, but have no meaning to users, and cannot quickly determine whether this user is a 'newcomer today' or 'long time no see'.

toLastLoginFormatted as "2023 March 15 10:30:00", it not only enhances the user-friendliness of data display but also facilitates website administrators in quickly assessing user activity.

IntroductionstampToDateLabel: Timestamp Translator

Of Security CMSstampToDateTags are used to convert the original timestamp (usually a 10-digit integer) into a date and time string in a specified format. Its usage is very intuitive:

{{stampToDate(时间戳, "格式")}}

The key here lies in the 'format' parameter, which follows the unique time formatting standard of the Go language (the development language of Anqi CMS). In simple terms, you need to use a specific date and time template instead of the way PHP or JavaScript do.Y-m-d H:i:sSuch placeholder.

Go language time formatting standard:

  • 2006Represents the year (e.g., 2023)
  • 01Represents the month (e.g., 03)
  • 02Represents the date (e.g., 15)
  • 15Represents the hour (24-hour format, e.g., 10)
  • 04represents minutes (such as 30)
  • 05represents seconds (such as 00)

Therefore, if you want to display the complete format 'Year-Month-Day Hour:Minute:Second', you need to use"2006-01-02 15:04:05"as the format string.

how to convertLastLoginFormat as readable date

to beLastLoginTimestamp formatting, we first need to go throughuserDetailGet this timestamp using tags, then pass it as a parameter tostampToDate.

Step one: GetLastLogin timestamp

Assuming we want to display the last login time of the user with ID 1. We can useuserDetailtag to obtainLastLoginthe value of the field and assign it to a temporary variable, such aslastLoginStamp.

{% userDetail lastLoginStamp with name="LastLogin" id="1" %}
    {# 此时,lastLoginStamp 变量中存储的就是用户ID为1的上次登录时间戳 #}
    <p>用户ID为1的上次登录时间 (原始时间戳):{{ lastLoginStamp }}</p>
{% enduserDetail %}

Or if the current page context has provided a complete user object (for example, on the user's personal center page, assuming the user object is nameduser), you can directly access itsLastLoginattribute:

{# 假设当前页面已有一个名为 'user' 的完整用户对象 #}
{% if user %}
    <p>当前用户上次登录时间 (原始时间戳):{{ user.LastLogin }}</p>
{% endif %}

Step two: ApplystampToDateFormat the tag

Now, we will pass the timestamp variable we have obtained tostampToDateand specify the date format we want.

”`twig {# Assume we want to display the last login time for user ID 1 #} {% userDetail lastLoginStamp with name=“LastLogin” id=“1” %}

<p>用户ID为1的上次登录时间 (原始时间戳):{{ lastLoginStamp }}</p>
<p>上次登录时间 (年-月-日):{{ stampToDate(lastLoginStamp, "2006-01-02") }}</p>
<p>上次登录时间 (完整日期时间):{{ stampToDate(lastLoginStamp, "2006-01-02 15:04:05") }}</p>
<p>上次登录时间 (中文格式):{{ stampToDate(lastLoginStamp, "2006年01月02日 15时04分") }}</p>

{% enduserDetail %}

{# If the current page already has a complete user object, you can directly access its LastLogin attribute #} {# Assume the current user object is 'user' and ensure it exists #} {% if user %}

<p>当前用户上次登录时间