Method 1: Get the current date and time in the PHP code before inserting the database
By default, PHP interprets the display time as "Greenwich Mean Time", which is 8 hours different from our local time.
So the time obtained by date ("Y-m-d h: I: s") is 8 hours less than the current time.
The time obtained by the gmdate ("Y-m-d h: I: s") function is calculated by addition or subtraction with GMT to obtain the local time.
In PHP. ini, the date. timezone option is used to set the time zone. However, the time zone is disabled by default and cannot be set in some PHP versions.
Normal implementation.
A better implementation method is to manually correct the time difference as follows:
$ Timeoffset = 8;
Echo gmdate ("Y-m-d h: I: s", mktime () + $ timeoffset * 3600). "<br> ";
Echo gmdate ("Y-m-d h: I: s", time () + $ timeoffset * 3600). "<br> ";
Echo date ("Y-m-d h: I: s", mktime () + $ timeoffset * 3600). "<br> ";
Echo date ("Y-m-d h: I: s", time () + $ timeoffset * 3600). "<br> ";
Output Format: 17:41:58
$ T = gmdate ("Y-m-d h: I: s", mktime () + 8*3600)
$ SQL = "insert into xxxx (adddatetime) values ($ t )"
Method 2: use MySQL built-in functions in SQL statements to obtain the current date and time
Use the now () function
Insert into xxxx (adddatetime) values (now ())
Summary:
Compared with the above two methods, it is better to use MySQL built-in functions
For more information, see:
[Reprint] PHP date and time function library
[Reprint] Date () function of PHP Date and Time ()