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.
- 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());
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.
- 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); }
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'.
- 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
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.
- 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');
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Linux new version
SublimeText3 Linux latest version

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),
