Solution to the problem of MSSQL datetime field output in ThinkPHP template

During the development process using the ThinkPHP framework, if you encounter problems when the datetime field in the MSSQL database is output in the template, you can take the following solutions.

  1. Problem Description

In the ThinkPHP framework, when the value of the datetime field is obtained from the MSSQL database and output in the template, the format may be incorrect or displayed as a timestamp. This is because the storage format of the datetime field in the MSSQL database is different from PHP's default date and time format, causing problems when outputting in the template.

  1. Solution

In order to solve this problem, we can use the time and date processing functions provided by the ThinkPHP framework to format the datetime field so that it can be output correctly in the template.

First, we need to define an accessor in the model to format the datetime field. Add a method that starts with get, names the field name in camel case, and adds the Attr suffix to the corresponding field in the model class. For example, for a datetime field named create_time, you can add a getCreateTimeAttr() method.

namespace app\common\model;

use think\Model;

Guess you like

Origin blog.csdn.net/qq_33885122/article/details/133573471