date_parse() function • How To Split Date and Time in PHP

There is function date_parse() in PHP. It splits date and time and returns an associative array. Now, easily We can get the day, month, hours, etc. separately by using the key names.

date_parse() function example

<?php

$date = date("d-m-Y h:i:s");
$extracted_date = date_parse($date);
echo $extracted_date["year"];

?>

Print all extracted data by loop as key value pair

date_parse() function also returns some arrays. Therefore, you will get a warning when you try to print it with echo. So, apply the condition, if not an array then print them.

if you do not want to print array then do not define else condition.

<?php

$date = date("d-m-Y h:i:s");
$extracted_date = date_parse($date);
foreach($extracted_date as $key => $result)
{
	if(!is_array($result))
	{
		echo $key." = ".$result."<br>";
	}
	else{
		echo $key." = ";print_r($result);echo "<br>";
	}
}

?>

Also, read –
** Print time-zones list in PHP
** calculation to date and time
** Print maximum number from an array in JS
** json_encode() function in PHP
** array to string conversion
** Replace existing class in Javascript
** SQL select query
** Add column in a table
** Drop database statements are disabled??

Leave a Comment