How to Fix Warning from Date() in PHP"

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



Leave a reply



Submit