When operating your website, if you provide member or VIP services, it is crucial for users to clearly understand their VIP status, when it will expire, and how to renew, as it is essential for improving user experience and member retention.AnQiCMS (AnQiCMS) takes advantage of its flexible template engine and powerful user management features, allowing you to easily implement these functions in website templates.

We all know that Anqi CMS is built with a comprehensive user group management and VIP system, which allows us to easily set different levels of user permissions and paid content.How can users intuitively understand all this when they see their VIP status?This is where the template is used inExpireTimeField.

UnderstandExpireTimefield

In Anqi CMS, each user has a relatedExpireTimeThe field records the expiration time of the user's VIP service.This field stores a standard timestamp (Unix timestamp), representing the moment when the VIP service expires.

To get this information in the template, we need to useuserDetailThis template tag. It can retrieve the detailed information of the current or specified user. Usually, we would do this to get the details of the currently logged in user.ExpireTime:

{% userDetail userProfile with id=user.Id %}
{% set expireTime = userProfile.ExpireTime %}

here,user.IdIt usually represents the ID of the currently logged in user.userProfileThis is an object that contains all the user's detailed information. ThroughuserProfile.ExpireTimewe obtained this critical timestamp.

This timestamp is not直观 when displayed directly, so we need to convert it into a date format that humans can understand. Anqi CMS providesstampToDateThis powerful filter (or function tag) can format timestamps into any date and time format you want. For example, if we want to format it as "2006-01-02 15:04", we can use it like this:

{% set formattedExpireTime = stampToDate(expireTime, "2006年01月02日 15时04分") %}

The core logic for determining VIP status

Now we have the user's VIP expiration timeexpireTimeand its readable formatformattedExpireTime. Next, we need to get the current time to compare withexpireTimeCompare. The Anqi CMS template providesnowtags to get the current time. For convenience, we usually also convert it to a timestamp.

{% set current_timestamp_str = now "20060102150405" %}
{% set current_timestamp = current_timestamp_str|integer %}

Here we utilizenowTag outputs a pure number date and time string (for example, "20231027103000"), then throughintegerThe filter converts it to a comparable integer timestamp.

With these two timestamps, we can use them in the template.if/elif(else if) andelseLogical judgment tags can be used to display different content according to VIP status.

Basic judgment logic can be divided into three cases:

  1. VIP status is normal: expireTimegreater thancurrent_timestamp.
  2. VIP is about to expire: expireTimegreater thancurrent_timestampBut it is also within a preset "about to expire" time range (such as within 7 days).
  3. VIP has expired: expireTimeLess than or equal tocurrent_timestamp.

In order to implement the judgment of "about to expire", we still need to calculate the timestamp of a "future point in time".For example, if you want to determine whether it will expire within 7 days, you can calculate the current timestamp plus the number of seconds corresponding to 7 days (7 * 24 * 60 * 60).

{% set seven_days_in_seconds = 7 * 24 * 3600 %}
{% set seven_days_later_timestamp = current_timestamp|add:seven_days_in_seconds %}

Here we cleverly utilizedaddThe filter to calculate the addition of timestamps

Implementing different states in the template

Now, let's combine these logic, let's see how to design different displays for these three states.

Assuming we want to display this information on the user's personal center page, you can refer to the following structure:

”`twig {# Assuming user.Id represents the current logged-in user's ID #} {% userDetail userProfile with id=user.Id %} {% set expireTime = userProfile.ExpireTime %} {# User's VIP expiration timestamp #}

{% set formattedExpireTime = stampToDate(expireTime, “January 2, 2006”) %} {# Format expiration date #}

{% set current_time_str = now “20060102150405” %} {# 获取当前时间(格式化为纯数字字符串) #} {% set current_timestamp = current_time_str|integer %} {# 转换为整数时间戳 #}

{% set seven_days_in_seconds = 7 * 2