Insert the current date and time into the MySQL database

Source: Internet
Author: User

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 ()

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.