PHP Programming Web Development

How to Get Current Date and Time(NOW) Using PHP

How to Get Current Date and Time(NOW) Using PHP June 8, 2017Leave a comment

Hello World, I am Chandra Kumar from New Delhi, India. I am professionally developer, programmer and blogger. I am the founder of WebShouters.com.

In this tutorial, we will show how you can get the current date and time in PHP. Use the following php code to get current date and time with Y-m-d and H:i:s or other format.

To get current datetime (now) with PHP you can use date with any PHP version, or better datetime class with PHP >= 5.2. You can find many date format expressions are available here.

PHP Date function help us to work with dates. Use new date() to get current date and time.

Using date() Function:

The below php script will return current date time in various format using date() function.

<?php
	$today = date("F j, Y, g:i a");                 // March 10, 2001, 5:16 pm
	$today = date("m.d.y");                         // 03.10.01
	$today = date("j, n, Y");                       // 10, 3, 2001
	$today = date("Ymd");                           // 20010310
	$today = date('h-i-s, j-m-y, it is w Day');     // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
	$today = date('\i\t \i\s \t\h\e jS \d\a\y.');   // it is the 10th day.
	$today = date("D M j G:i:s T Y");               // Sat Mar 10 17:16:18 MST 2001
	$today = date('H:m:s \m \i\s\ \m\o\n\t\h');     // 17:03:18 m is month
	$today = date("H:i:s");                         // 17:16:18
	$today = date("Y-m-d H:i:s");                   // 2001-03-10 17:16:18 (MySQL DATETIME format)
?>

Using datetime Class:

The below php script will return current date time in various format using datetime class.

<?php
	$dt = new DateTime();
	$today = $dt->format("F j, Y, g:i a");                 // March 10, 2001, 5:16 pm
	$today = $dt->format("m.d.y");                         // 03.10.01
	$today = $dt->format("j, n, Y");                       // 10, 3, 2001
	$today = $dt->format("Ymd");                           // 20010310
	$today = $dt->format('h-i-s, j-m-y, it is w Day');     // 05-16-18, 10-03-01, 1631 1618 6 Satpm01
	$today = $dt->format('\i\t \i\s \t\h\e jS \d\a\y.');   // it is the 10th day.
	$today = $dt->format("D M j G:i:s T Y");               // Sat Mar 10 17:16:18 MST 2001
	$today = $dt->format('H:m:s \m \i\s\ \m\o\n\t\h');     // 17:03:18 m is month
	$today = $dt->format("H:i:s");                         // 17:16:18
	$today = $dt->format("Y-m-d H:i:s");                   // 2001-03-10 17:16:18 (MySQL DATETIME)
?>

References:

http://php.net/manual/en/function.date.php
http://php.net/manual/en/intro.datetime.php

How to Get Current Date and Time(NOW) Using PHP
5 (100%) 2 votes

(Visited 2 times, 1 visits today)

Hello World, I am Chandra Kumar from New Delhi, India. I am professionally developer, programmer and blogger. I am the founder of WebShouters.com.

Leave a Reply

Your email address will not be published. Required fields are marked *