search
HomeBackend DevelopmentPHP Tutorial PHP date("Y-m-d H:i:s");获取当前时间 差8小时解决方法(转)

PHP date("Y-m-d H:i:s");获取当前时间 差8小时解决办法(转)

PHP date("Y-m-d H:i:s");获取当前时间 差8小时解决办法

(转)http://subin.org.cn/blog/post/430.html

原因:?? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?

??????? 从php5.1.0开始,php.ini里加了date.timezone这个选项,并且默认情况下是关闭的也就是显示的时间(无论用什么php命令)都是格林威治标准时间和我们的时间(北京时间)差了正好8个小时。

??????? 关于timezone 大陆内地可用的值是:Asia/Chongqing ,Asia/Shanghai ,Asia/Urumqi ,PRC(依次为重庆,上海,乌鲁木齐,中华人民共和国)港台地区可用:Asia/Macao ,Asia/Hong_Kong ,Asia/Taipei (依次为澳门,香港,台北)还有新加坡:Asia/Singapore其它:Etc/GMT-8 ,Singapore ,Hongkong

解决方法一:

??????? 再添加时间前 插入:CODE:(好像不用加)?date_default_timezone_set(PRC);

解决方法二:

??????? echo date("Y-m-d H:i:s",?time()+8*60*60);

解决方法三(修改php.ini文件):

??????? 找到php.ini中的“;date.timezone =”这行,将“;”去掉,改成“date.timezone = PRC”(PRC:People's Republic of China 中华人民共和国),重启Apache,问题解决。

?

附:date时间格式

date("Y-m-d H:i:s"); 显示的格式: 年-月-日 小时:分钟:秒

相关时间参数:

a - "am" 或是 "pm"?
A - "AM" 或是 "PM"?
d - 几日,二位数字,若不足二位则前面补零; 如: "01" 至 "31"?
D - 星期几,三个英文字母; 如: "Fri"?
F - 月份,英文全名; 如: "January"?
h - 12 小时制的小时; 如: "01" 至 "12"?
H - 24 小时制的小时; 如: "00" 至 "23"?
g - 12 小时制的小时,不足二位不补零; 如: "1" 至 12"?
G - 24 小时制的小时,不足二位不补零; 如: "0" 至 "23"?
i - 分钟; 如: "00" 至 "59"?
j - 几日,二位数字,若不足二位不补零; 如: "1" 至 "31"?
l - 星期几,英文全名; 如: "Friday"?
m - 月份,二位数字,若不足二位则在前面补零; 如: "01" 至 "12"?
n - 月份,二位数字,若不足二位则不补零; 如: "1" 至 "12"?
M - 月份,三个英文字母; 如: "Jan"?
s - 秒; 如: "00" 至 "59"?
S - 字尾加英文序数,二个英文字母; 如: "th","nd"?
t - 指定月份的天数; 如: "28" 至 "31"?
U - 总秒数?
w - 数字型的星期几,如: "0" (星期日) 至 "6" (星期六)?
Y - 年,四位数字; 如: "1999"?
y - 年,二位数字; 如: "99"?
z - 一年中的第几天; 如: "0" 至 "365"

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

PHP Email Security: Best Practices for Sending EmailsPHP Email Security: Best Practices for Sending EmailsMay 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools