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" (LastLoginis a direct measure of user activity.However, this data is usually stored in the form of raw timestamps, and a series of numbers is far less friendly and understandable to ordinary users than a direct date and time.stampToDateLabel is the tool to solve this problem, it can help us easily convert the abstract timestamp into a clear and readable date format.
UnderstandingLastLoginand its importance
In the template development of AnQi CMS, you may encounter it on the user detail page, member center, or modules that need to display user activity informationLastLoginThis field records the system time of the last successful login of the user, which is usually a 10-digit or 13-digit integer (Unix timestamp). For example,1678886400Such numbers are precise, but they mean nothing to users and cannot quickly determine whether this user is 'new today' or 'hasn't logged in for a long time'.
toLastLoginFormatted as "2023 March 15 10:30:00", it not only improves the user-friendliness of data display, but also facilitates website administrators in quickly evaluating user activity.
IntroductionstampToDate标签:时间戳的“English”翻译官
Anqi CMS'sstampToDateLabel is 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(时间戳, "格式")}}
Here lies the key to 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, rather than using like PHP or JavaScript.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 hour (24-hour format, such as 10)04Represents minutes (such as 30)05Represents seconds (such as 00)
Therefore, if you want to display the complete format of “Year-Month-Day Hour:Minute:Second”, you need to use"2006-01-02 15:04:05"as the format string.
How to applyLastLoginFormatted into a readable date
To addLastLoginTimestamp formatting, we first need to go throughuserDetailLabel gets this timestamp and then passes it as a parameter tostampToDateLabel.
Step one: GetLastLoginTimestamp
Suppose we want to display the last login time of the user with ID 1. We can useuserDetailtags to getLastLoginField value, and assign it to a temporary variable, for examplelastLoginStamp.
{% userDetail lastLoginStamp with name="LastLogin" id="1" %}
{# 此时,lastLoginStamp 变量中存储的就是用户ID为1的上次登录时间戳 #}
<p>用户ID为1的上次登录时间 (原始时间戳):{{ lastLoginStamp }}</p>
{% enduserDetail %}
or, if the current page context has already provided a complete user object (for example, on the user's personal center page, assuming the user object is nameduser), you can directly access itsLastLoginProperties:
{# 假设当前页面已有一个名为 'user' 的完整用户对象 #}
{% if user %}
<p>当前用户上次登录时间 (原始时间戳):{{ user.LastLogin }}</p>
{% endif %}
步骤二:EnglishstampToDateTags are used for formatting.
现在,我们将获取到的时间戳变量传递给stampToDate,并指定我们想要的日期格式。
`twig {# 假设我们想要显示用户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 %} English
{# If the current page already has a complete user object, it can directly access its LastLogin property #} {# Assuming the current user object is 'user' and ensuring it exists #} {% if user %}
<p>当前用户上次登录时间