Is the `created_time` and `updated_time` field of the document in Unix timestamp, how to perform time format conversion?

Calendar 👁️ 85

In AnQiCMS (AnQiCMS), when you retrieve various information such as documents, categories, attachments, users, and more through the API interface, you will find that likecreated_time(creation time) andupdated_time(Update time) such fields are usually presented in integer form. This is indeed the common Unix timestamp format used by many systems, including AnQiCMS.

What is a Unix timestamp?

Unix timestamp, also known as POSIX time or Epoch time, is a way of representing time that represents time as the number of seconds elapsed since 00:00:00 UTC on January 1, 1970, which is the Unix epoch, Unix Epoch.It is a pure number, which does not contain any time zone or localization information, and therefore has a very high degree of universality and consistency in cross-system, cross-language data exchange.AnQiCMS uses this format to facilitate data storage, transmission, and flexible handling in different application environments.

Why does AnQiCMS use Unix timestamps?

There are several significant advantages to using Unix timestamps:

  • High storage efficiency:Compared to complex date-time strings, integer-type Unix timestamps take up less storage space.
  • Calculation is convenient:Date and time interval calculation (for example, calculating how many days since the item was released) can be completed by simple integer addition and subtraction.
  • Cross-platform compatibility:Almost all programming languages and database systems natively support the parsing and conversion of Unix timestamps, reducing the barriers to time data processing between different systems.
  • Time zone independence:The timestamp itself does not contain timezone information, it always represents UTC time, which means you can get it anywhere and convert it to any specific timezone time as needed.

How to convert Unix timestamp to a readable date and time format?

Although Unix timestamps are very efficient in machine processing, for users, a string of numbers is obviously not as intuitive as the format "October 27, 2023, 14:30:00".Therefore, in front-end display or back-end business logic, we usually need to convert it into a date and time string that is easier to understand.

Here are several common conversion methods in various programming languages:

1. JavaScript (Frontend or Node.js)

JavaScript'sDateThe object accepts millisecond timestamps by default. Since AnQiCMS returns second-level timestamps, you need to multiply it by 1000.

const unixTimestamp = 1662717106; // 示例:来自AnQiCMS的updated_time
const date = new Date(unixTimestamp * 1000);

// 转换为本地时间字符串
console.log(date.toLocaleString()); // 例如: "2022/9/9 下午1:51:46" (根据您的系统设置)

// 转换为特定格式(例如:YYYY-MM-DD HH:mm:ss)
const year = date.getFullYear();
const month = (date.getMonth() + 1).toString().padStart(2, '0'); // 月份从0开始
const day = date.getDate().toString().padStart(2, '0');
const hours = date.getHours().toString().padStart(2, '0');
const minutes = date.getMinutes().toString().padStart(2, '0');
const seconds = date.getSeconds().toString().padStart(2, '0');

const formattedDate = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
console.log(formattedDate); // 例如: "2022-09-09 13:51:46"

2. PHP (Backend)

PHP'sdate()The function can directly handle Unix timestamps, the second parameter is the timestamp.

$unixTimestamp = 1662717106; // 示例:来自AnQiCMS的updated_time

// 转换为可读格式
$formattedDate = date('Y-m-d H:i:s', $unixTimestamp);
echo $formattedDate; // 例如: "2022-09-09 13:51:46"

3. Python (Backend)

Python can be useddatetimeto convert modules.

import datetime

unix_timestamp = 1662717106  # 示例:来自AnQiCMS的updated_time

# 将时间戳转换为datetime对象
dt_object = datetime.datetime.fromtimestamp(unix_timestamp)

# 格式化为字符串
formatted_date = dt_object.strftime('%Y-%m-%d %H:%M:%S')
print(formatted_date) # 例如: "2022-09-09 13:51:46"

4. Go (AnQiCMS underlying language, backend)

If you are developing in Go language, AnQiCMS itself is written in Go, and its time handling is consistent with this.

package main

import (
	"fmt"
	"time"
)

func main() {
	unixTimestamp := int64(1662717106) // 示例:来自AnQiCMS的updated_time

	// 将秒级时间戳转换为time.Time对象
	t := time.Unix(unixTimestamp, 0)

	// 格式化为字符串
	formattedDate := t.Format("2006-01-02 15:04:05") // Go语言独特的格式化参考时间
	fmt.Println(formattedDate) // 例如: "2022-09-09 13:51:46"
}

What should be noted when converting?

When performing timestamp conversion, there are several details to pay attention to:

  • Time zone:Unix timestamp is based on UTC, but when converted to a string, many library functions (such as JavaScript'stoLocaleString()) It will use the time zone of your system by default. If you need to display a specific time zone (such as the time zone of the server or fixed to UTC), please make sure to specify it clearly when converting or formatting.For example, of JavaScript'stoUTCString()ortoISOString()methods can obtain UTC time.
  • Millisecond vs. Second:To emphasize again, AnQiCMS returnssecondstimestamp, while JavaScript'sDateconstructor needsmilliseconds. Remember to multiply by 1000 in JavaScript. Other languagesfromtimestampordatefunctions usually handle second-level timestamps by default.
  • Formatting string:Different programming languages have their own placeholder conventions for formatting date and time strings (for exampleY-m-d/%Y-%m-%d/2006-01-02etc.), please refer to the documentation of the corresponding language.

By following the above methods and precautions, you can easily convert the Unix timestamp provided by AnQiCMS into various readable date and time formats, thereby better displaying and processing time information on your website or application.

Frequently Asked Questions (FAQ)

  1. Why does AnQiCMS not return formatted time strings directly but use Unix timestamps instead?The use of Unix timestamps has many technical advantages, such as higher database storage efficiency, ease of cross-language and cross-system data exchange, and direct calculation of time intervals.What is more important is that it is unrelated to specific time zones and display formats, which provides developers with great flexibility to freely convert timestamps to any desired date-time format and time zone on either the client or server, according to different application scenarios and user needs.

  2. What is in the document?created_timeorupdated_timeWhat does it mean if the field is displayed as 0?Answer: Ifcreated_timeorupdated_timeThe value is 0, usually indicating that the time information of the document or record has not been set or initialized.In Unix timestamps, 0 represents the Unix epoch (UTC January 1, 1970, 00:00:00).In practical applications, if such a situation is encountered, it may be necessary to check the data source or system configuration to see why these fields are not assigned correctly.

  3. Can I directly set the display format of these time fields in the AnQiCMS backend?Answer: The AnQiCMS backend usually does not provide a direct function to change the timestamp format of the API response, as the API design tends to provide raw, general data formats.If you want to display time in a specific format on the website frontend or a custom template, you need to use the programming language methods mentioned above (such as JavaScript, PHP) to convert and format the timestamp returned by the API to achieve the display effect you want.

Related articles

What is the role of `canonical_url` and `fixed_link` in document details for SEO and link management?

In website operation, the health status of links and the friendliness to search engines are the foundation for the success of the website.AnQi CMS provides us with two key fields: `canonical_url` and `fixed_link`, which play a crucial role in document details and help us optimize the search engine performance of the website and effectively manage links. ### canonical_url: Define the "main version" of the content Imagine that your website might have multiple URLs pointing to the same or highly similar content for various reasons.

2025-11-09

How to interpret the `flag` recommended attribute in the document details, for example, what do `h`, `c`, `f`, and others represent?

In AnQi CMS, the recommended attribute `flag` is a very practical feature in content operations. It allows us to classify and label document content, thereby enabling personalized display and filtering in different areas of the website.Understanding the meaning of these brief letter codes can help us manage and present website content more efficiently.Where can I find the `Flag` attribute? This important `flag` attribute appears in several key interfaces, it is both the characteristic of the document itself, and the important basis for querying and filtering documents. First

2025-11-09

What do the `logo` and `thumb` fields represent in the document details, and what is the difference between them?

When managing content in Anqi CMS, we often encounter questions about image fields, especially the `logo` and `thumb` fields, which are both involved in image representation in the document detail interface (`archiveDetail`), but have subtle yet important differences in actual use and meaning.Understanding their differences can help us better optimize the visual presentation and loading performance of the website.### `logo` field: the "identity card" of the document When we talk about the `logo` field

2025-11-09

How is the `images` field (document group image) stored and parsed in the document details?

When using AnQiCMS to manage website content, images are undoubtedly an important element in enhancing the attractiveness of the content, especially when we want to add a series of related images to the document, the album feature becomes particularly crucial.So, how are these group images stored and parsed in the AnQiCMS document details?Understanding this will help us better utilize AnQiCMS for content creation and display.### The storage mechanism of document group charts First

2025-11-09

The value of the `status` field in the document details, for example 1, specifically indicates which display status the document has?

When using AnQi CMS to manage website content, you may encounter various technical parameters, one of which seems simple but is crucial is the `status` field in the document details.It acts like a 'switch' for the content, quietly controlling whether your article, product, or other information can be seen by visitors. Then, what does this `status` field specifically represent?In simple terms, it is an integer value used to mark the current display state of the document.

2025-11-09

If the document is a product type, how can you get its `price` (price) and `stock` (stock) information?

In Anqi CMS, if you are managing documents of product types and need to obtain specific price (`price`) and stock (`stock`) information for these products, it is actually very direct and convenient.The design of AnQi CMS considers such needs, making these key data as one of the core attributes of the document, you can easily obtain them through several core interfaces.### Get the price and stock information of a single product document When you need to view the price and stock of a specific product, AnQi CMS provides the `archiveDetail` interface

2025-11-09

What detailed information will the nested `category` object return?

When we explore the mysteries of content in AnQi CMS, we often need to obtain detailed information about the documents.The `archiveDetail` interface provides us with rich document data, and the nested `category` object is a frequently overlooked but extremely important repository of information.It is not just a simple classification ID, but a complete entity containing all the metadata of the classification, which provides great convenience for us to deeply understand the context of the document and to carry out refined content display.

2025-11-09

How to display the `extra` object, how to access its `name` and `value`?

When using AnQiCMS to manage website content, the flexibility of custom fields in the document model is one of its highlights.These custom fields allow us to add unique properties for different types of documents (such as articles, products, etc.), greatly enriching the content dimensions.When obtaining document details or lists through the API, the information of these custom fields is cleverly encapsulated in the `extra` object of the returned data.

2025-11-09