PHP Formatting Date

To display current date we use the date function in PHP.


<?php
	//string date  ( string $format  [, int $timestamp  ] )
	echo date("Y-m-d H:i");
?>

Result:

The date() function requires 1 parameter which is the string format to format the current date time to our liking. Refer to the php date documentation for more details on the formatting string. The 2nd optional parameter which takes in an integer value representing the date time comes in handly where in some situation, we need to display specific dates. We use mktime() function to create the specific date time.


<?php
	//int mktime  ([ int $hour = date("H")  [, int $minute = date("i")  [, int $second = date("s")  [, int $month = date("n")  [, int $day = date("j")  [, int $year = date("Y")  [, int $is_dst = -1  ]]]]]]] )

	//mktime(8,0,0,10,20,2010) creates a date time specific to 20th October 2010, 8:00 am
	echo date("Y-m-d H:i",mktime(8,0,0,10,20,2010)); 
?>

Result: