Home >PHP Framework >ThinkPHP >How to display time format in ThinkPHP
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.
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());
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.
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); }
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;
will output the time in the format of '2021-09-22 15:00:00'.
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
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();
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.
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');
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!