How to get the current time and date using How to get current time and date using php? In PHP, we can use the date() function to get the current time and date, or we can use the DateTime PHP class in PHP>5.2 to get the date and time. Let's take a look at the specific content.
Use the date() function to output the current date and time. It will use the default time zone configured in How to get current time and date using php.ini.
<?How to get current time and date using php echo date('Y-m-d H:i:s'); ?>
Use the date time() class to output the current date and time. It will also use the default time zone.
<?How to get current time and date using php $datetime = new DateTime(); echo $datetime->format('Y-m-d H:i:s'); ?>
Date and time in PHP time zone:
It is also possible to define a specific time zone for the application. Now PHP will not use the system defined time zone.
<?How to get current time and date using php date_default_timezone_set('UTC'); $datetime = new DateTime(); echo $datetime->format('Y-m-d H:i:s'); ?>
Convert Date Time Time Zone
For example, you have stored the date (2018-01-11 01:17:23) in UTC time zone in the database. Now convert this DateTime to EST time zone.
<?How to get current time and date using php $date = new DateTime('2011-11-10 20:17:23', new DateTimeZone('UTC')); $date->setTimezone(new DateTimeZone('EST')); echo $date->format('Y-m-d H:i:s'); ?>
After executing the above command, you will get the following results.
2018-01-11 01:17:23
This article ends here. For more exciting content, you can pay attention to other related column tutorials on the PHP Chinese website! ! !
The above is the detailed content of How to get current time and date using php. For more information, please follow other related articles on the PHP Chinese website!