Insert time data to the database in YiiFramework

Source: Internet
Author: User
In the PHP code, obtain the current date time and insert it into the database. By default, PHP explains that the displayed time is "linwei governance standard time", which is 8 hours different from our local time, therefore, the time obtained by date ("Y-m-dH: I: s") is 8 hours less than the current time. The time obtained by the gmdate ("Y-m-dH: I: s") function is subtracted from GMT,

In the PHP code, obtain the current date time and insert it into the database. By default, PHP explains that the displayed time is "linwei governance standard time", which is 8 hours different from our local time, therefore, 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 subtracted from GMT,

Zookeeper

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 gmdate ("Y-m-d H: I: s") function obtains the local time after addition or subtraction with GMT.
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) . "
";echo gmdate("Y-m-d H:i:s", time() + $timeoffset * 3600) . "
";echo date("Y-m-d H:i:s", mktime() + $timeoffset * 3600) . "
";echo date("Y-m-d H:i:s", time() + $timeoffset * 3600) . "
";

Output Format: 17:41:58

Based on the above knowledge, I quickly inserted data. The Code is as follows:

Public function actionAdd () {$ news_model = new News (); $ news_model-> title = "6th News headlines tested "; $ news_model-> pubdate = date ("Y-m-d H: I: s", time () + 8*3600); $ news_model-> author = "yuanpengfei "; if ($ news_model-> save () {echo "success save" ;}else {echo "failed save ";}}

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.