How to format `LastLogin` (last login time) into a readable date using the `stampToDate` tag?

Calendar 👁️ 83

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>当前用户上次登录时间

Related articles

The `userDetail` tag supports which parameters to accurately find specific user information?

In AnqiCMS template development, to display specific user information on the page, we often use a very practical tag - `userDetail`.This tag allows us to accurately retrieve and present detailed user data based on specific conditions.Understanding the parameters it supports is crucial for flexibly building user-related page functions.The core function of the `userDetail` tag is to obtain data based on a clear user identifier.When in use, it requires us to specify the user ID to be queried through the `id` parameter

2025-11-07

How to use the `userDetail` tag to retrieve the user's real name `RealName` on the frontend page?

In website operations, personalization and user experience are the key to enhancing brand image.Sometimes, we are not satisfied with just displaying the user's nickname or avatar. In certain scenarios, such as the member center, profile page, or some activity leaderboard, we may also need to display the user's real name.AnQi CMS is a highly flexible and feature-rich Go language content management system that provides intuitive template tags, allowing front-end developers to easily retrieve and display this information.Today, let's delve into how to use the `userDetail` tag of Anqi CMS

2025-11-07

How to correctly display the user's `UserName` (username) in the AnQiCMS template?

In AnQiCMS templates, flexibly displaying user information is a key link to enhance website personalization and user experience.Whether it is to display the nickname of the comment author or to show the detailed information of the registered user on a specific page, it is very important to understand how to correctly call `UserName`. AnQiCMS's powerful template engine provides multiple ways to achieve this goal, and we will delve deeper into these methods next.AnQiCMS's template system adopts a syntax similar to the Django template engine, which allows developers to quickly build pages in a concise and efficient manner

2025-11-07

How to use the `userDetail` tag to obtain and display the ID information of a specified user?

AnQiCMS with its flexible and powerful template tag system makes the display of website content very convenient.Whether it is articles, products, or user information, they can be easily displayed on the page with simple tags.Today, let's talk about how to use the `userDetail` tag to accurately obtain and display the ID information of a specified user.

2025-11-07

How to dynamically display exclusive content for distributors based on the user's `IsRetailer` status in AnQiCMS templates?

Today, providing customized experiences for users of different types has become the key to improving user satisfaction and business efficiency.For businesses with a distribution system, how to ensure that distributors can easily access their exclusive content without causing interference to ordinary users is a common need.AnQiCMS with its flexible template engine and powerful user management features, can help you easily achieve this goal.

2025-11-07

How to get the user's account balance `Balance` and cumulative earnings `TotalReward` through the `userDetail` tag?

How to retrieve and display a user's account balance and cumulative earnings in AnQi CMS template?For operators, it is a key element to enhance user experience and manage the membership system by flexibly displaying user-specific information on the website, such as their account balance or cumulative earnings.AnQi CMS is a powerful and practical content management system that provides a very convenient way to obtain these user data.Today, let's talk about how to use your template

2025-11-07

Can the `userDetail` tag retrieve the user's email `Email` and phone number `Phone` and display it on the front end?

In website operation and user experience design, sometimes we need to display specific user information on the front-end page, such as contact email or phone number, in order to provide personalized services or facilitate users to view/update their personal information.It is crucial for users of AnQiCMS to understand how to implement this safely and efficiently.This article will delve into the `userDetail` tag of AnQiCMS, discussing whether it can retrieve and display the user's email (`Email`) and phone number (`Phone`), as well as the matters that need to be paid attention to during use

2025-11-07

How to use `AvatarURL` or `FullAvatarURL` to display a user avatar in the template?

In website operation, the user avatar is not just a decoration, it carries the user's identity and is an important part of community interaction and personalized experience.Whether it is in the comment area, author introduction, or member center, a clear user avatar can significantly enhance the website's affinity and professionalism.AnQiCMS as an efficient content management system naturally also provides us with the powerful function of flexibly displaying user avatars in templates.Today, let's talk about how to use the `AvatarURL` and `FullAvatarURL` fields in AnQiCMS templates

2025-11-07