In the daily operation of Anqi CMS, we often need to handle time-related fields, such as the expiration time of the user's VIP (ExpireTime)。Correctly display these time information, and make flexible conditional judgments according to their status, which is crucial for improving user experience and implementing personalized content display. This article will focus onExpireTimefield, discuss in detail how to utilizestampToDatetag for date formatting, combined with conditional judgment, to implement intelligent management of VIP status.
UnderstandingExpireTimethe meaning of the field
In the Anqi CMS user group management and VIP system,ExpireTimeThe field usually records the specific expiration time of VIP services.This field does not store a common readable date string, but a Unix timestamp (usually a 10-digit integer), which represents the number of seconds from 00:00:00 UTC/GMT on January 1, 1970 to the specified date and time.This storage method is convenient for the system to perform time calculation and comparison, but when displayed on the front-end, we need to convert it to a user-friendly date format.
UsestampToDatetag for date formatting
AnQi CMS providesstampToDateThe tag is specifically used to convert Unix timestamps into various readable date and time formats. Its usage is intuitive and concise:
{{ stampToDate(时间戳, "格式") }}
Among them,时间戳which we get fromExpireTimethe field, and格式It follows the standard time formatting string of Golang (the development language of AnQiCMS). Golang's time formatting is not like PHP or Java.Y-m-dIt is the point in time at which the format is defined instead of using a placeholder, that is“2006-01-02 15:04:05.999999999 -0700 MST”. You just need to write according to the corresponding part of this reference time and you will get the format you want.
For demonstration purposes, we assume that you have already passed.userDetailThe label obtained the current user's VIP information and assigned it touserProfileVariable:
`twig {% userDetail userProfile with id=currentUser.Id %} {# Assume currentUser.Id is the current user's ID #}
{% set rawExpireTime = userProfile.ExpireTime %} {# rawExpireTime现在是原始的Unix时间戳 #}
<p>原始到期时间戳:{{ rawExpireTime }}</p>
<p>仅显示到期日期:{{ stampToDate(rawExpireTime, "2006年01月02日") }}</p>
{# 结果示例:2024年09月30日 #}
<p>显示精确到秒的到期时间:{{ stampToDate(rawExpireTime, "2006-01-02 15:04:05") }}</p>
{# 结果示例:20