Friday 14 December 2012

Convert string to date in PHP


Sometimes it will need to convert the date in string format to convert it in to the original DATETIME format. In PHP we can do this like the following.

<?PHP
 
$date = "29/11/2012 23:26"; 
 
$newDate = preg_replace("/(\d+)\D+(\d+)\D+(\d+)/","$3-$2-$1",$date); 
 
echo date('Y-m-d H:i:s',strtotime($newDate)); 
 
?>

Hope this helps!

No comments:

Post a Comment