Want create site? Find Free WordPress Themes and plugins.

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

Did you find apk for android? You can find new Free Android Games and apps.
1 Star2 Stars3 Stars4 Stars5 Stars (1 votes, average: 5.00 out of 5)
Loading...

Posted by Editorial Staff