How to fix warning from date() in PHP
Try to set date.timezone
in php.ini
file. Or you can manually set it using ini_set()
or date_default_timezone_set()
.
Can I avoid `DateTime::__construct` warning on invalid date?
This is possibly one of the few cases where it's acceptable to use:
try {
$var = @new DateTime('some invalid date format');
} catch (Exception $e) {
$var = null;
}
However you can avoid the @
operator by doing:
try {
$oldErrorReporting = error_reporting();
error_reporting($oldErrorReporting & ~E_WARNING);
$var = new DateTime('some invalid date format');
error_reporting($oldErrorReporting);
} catch (Exception $e) {
$var = null;
}
PHP warning on date and time
Your php.ini setting should look like:
date.timezone = "Asia/Tehran"
... then restart Apache (if php is running as CGI, FCGI or Apache module)
If you're using PHP-FPM make sure to restart it:
service php-fpm restart
Alternatively you can set it in your "index.php":
<?php
date_default_timezone_set("Asia/Tehran");
If you don't see effects of changes made to php.ini, make sure you are editing the file that is used by php. You can check php.ini location used during runtime using this function:
<?php
phpinfo(INFO_GENERAL);
Warning: date() [function.date]:
it is because you do not have default time zone set in php.ini.
Add this line at the beginning of your PHP.
date_default_timezone_set('America/Los_Angeles');
p.s. of course, change to your time zone.
How to fix PHP errors related to timezone (function.strtotime and function.date)
date.timezone
in php.ini
can fix this globally.
Related Topics
How to Eliminate PHP5 Strict Standards Errors
Flat PHP Array to Hierarchy Tree
Pretty Urls Without Mod_Rewrite, Without .Htaccess
How to Find Annotations in a PHP5 Object
Which Is the Best Character Encoding for Japanese Language for Db, PHP, and HTML Display
Unpacking an Array of Arguments in PHP
How to Prevent JavaScript Code Theft
What Is Difference of Developing a Website in MVC and 3-Tier or N-Tier Architecture
How to Loop Through JSON Array
500 Server Error: Premature End of Script Headers:
Allow Cross Domain Ajax Requests
Mysql_Connect (Localhost/127.0.0.1) Slow on Windows Platform