How to Get Current Date and Time in PHP

Channel: Linux
Abstract: 26 date('Y-m-d Hphp $currentDateTime = date('Y-m-d H

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.

<?php $currentDateTime = date('Y-m-d H:i:s'); echo $currentDateTime; ?>1234<?php    $currentDateTime = date('Y-m-d H:i:s');    echo $currentDateTime;?>

Result:-

2017-02-27 08:22:26

Other useful examples:

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:261234  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.

<?php $dt = new DateTime(); echo $dt->format('Y-m-d H:i:s'); ?>1234<?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

Ref From: tecadmin

Related articles