You can use PHP date()
function or DateTime()
class to get current Date & Time in PHP. This tutorial will help you to get current date time in PHP.
The provided results based on the timezone settings in the php.ini file. You may need to modify this setting to get date and time in the required timezone. Read this tutorial to set timezone in PHP configuration.
Using date() Function
PHP date() function converts the timestamp stored in computer memory in a human-readable format. The syntax of date() function is as:
date(format, [timestamp])
The below php script will return current date time in ‘Y-m-d H:i:s’ format.
1 2 3 4 | <?php $currentDateTime = date('Y-m-d H:i:s'); echo $currentDateTime; ?> |
Result:-
2017-02-27 08:22:26
Other useful examples:
1 2 3 4 | date('Y/m/d'); // 2017/02/27 date('m/d/Y'); // 02/27/2017 date('H:i:s'); // 08:22:26 date('Y-m-d H:i:s'); // 2017-02-27 08:22:26 |
Using DateTime() Class:
The DateTime() class functions allow you to get the date and time. You can also use functions to format the date and time in many different ways.
1 2 3 4 | <?php $dt = new DateTime(); echo $dt->format('Y-m-d H:i:s'); ?> |
Result:-
2017-02-27 08:22:26
References:
http://php.net/manual/en/function.date.php
http://php.net/manual/en/intro.datetime.php
2 Comments
WAMP: showing me time 3 hours before time. Its 18:22:39 but it is showing time part 15:22:39.
My Local PC time is also fine and showing time by 18:22:39
Why ?
You may need to change timezone in PHP configuration:
https://tecadmin.net/setup-timezone-in-php-configuration/