Convert DateTime To String PHP


Answer :

You can use the format method of the DateTime class:



$date = new DateTime('2000-01-01');
$result = $date->format('Y-m-d H:i:s');


If format fails for some reason, it will return FALSE. In some applications, it might make sense to handle the failing case:



if ($result) {
echo $result;
} else { // format failed
echo "Unknown Time";
}


The simplest way I found is:



$date   = new DateTime(); //this returns the current date time
$result = $date->format('Y-m-d-H-i-s');
echo $result . "<br>";
$krr = explode('-', $result);
$result = implode("", $krr);
echo $result;


I hope it helps.



echo date_format($date,"Y/m/d H:i:s");


Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable