Home > PHP Framework > ThinkPHP > How to display time format in ThinkPHP

How to display time format in ThinkPHP

PHPz
Release: 2023-04-11 15:55:27
Original
1514 people have browsed it

ThinkPHP is an open source Web application framework based on PHP. With the development of the Internet, this framework has been widely used in many fields. When using ThinkPHP to develop web applications, time format is inevitable. This article will introduce how to display time format in ThinkPHP.

  1. Convert timestamp to time format

In ThinkPHP, we usually store time in the form of timestamp because it is more convenient and efficient. However, when we need to display the time on the page, we need to convert the timestamp into a time in a readable format.

In ThinkPHP, you can use PHP's built-in date() function to convert the timestamp into time format. This function contains two parameters: the first parameter is the time format, and the second parameter is the timestamp to be converted.

For example, if you want to convert the current timestamp to date format, you can use the following code:

echo date('Y-m-d H:i:s', time());
Copy after login

Where, 'Y-m-d H:i:s' is the time format and time() is the current timestamp.

This code will output the current date and time in the format of '2021-09-22 14:30:00'. You can change the time format according to your needs.

  1. Format the time field in the database

When using ThinkPHP, we usually save the time field in the database. If you want to display these time fields on the page, you need to format them.

A simple way is to use the getAttr() method in the model. This method is automatically called when reading a database field.

For example, if you have a time field named 'create_time', you can define the following method in the model:

public function getCreateTimeAttr($value)
{
    return date('Y-m-d H:i:s', $value);
}
Copy after login

This method will format the timestamp of the 'create_time' field as Standard date format. After using this method, you can call this field in the controller, for example:

$user = UserModel::get($id);
echo $user->create_time;
Copy after login

will output the time in the format of '2021-09-22 15:00:00'.

  1. Using the PHP Carbon extension

PHP Carbon is a PHP library that provides a simpler and more powerful way to manipulate dates and times. In ThinkPHP, you can also use PHP Carbon to format time.

First, you need to install this extension through Composer. Run the following command:

composer require nesbot/carbon
Copy after login

After the installation is complete, you can use the following code in your code:

use Carbon\Carbon;

$now = Carbon::now('Asia/Shanghai');
echo $now->toDateTimeString();
Copy after login

This code will output the current date and time and use 'Asia/Shanghai' as the time zone. You can change the time zone and format.

  1. Using the PHP DateTime class

PHP also provides a DateTime class, which can be used to manipulate dates and times. In ThinkPHP, you can use this class to format time.

The following is a sample code:

$dateStr = '2021-09-22 15:30:00';
$date = new DateTime($dateStr, new DateTimeZone('Asia/Shanghai'));
echo $date->format('Y-m-d H:i:s');
Copy after login

This code will output the time in the format of '2021-09-22 15:30:00'.

In short, displaying time format in ThinkPHP is very simple. You can use PHP's built-in functions, or use the PHP Carbon or DateTime classes to convert timestamps or time fields in the database.

The above is the detailed content of How to display time format in ThinkPHP. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template