Reputation: 765
I have some string like this.
$str = '14032017';
I want convert it to date format and show the result as 14-03-2017
I already try this but still didn't work :
<?php
$str = '14032017';
$strdate = strtotime($str);
$date = date('d-m-Y',$strdate);
echo $date;
?>
How could I to do that ?
Upvotes: 1
Views: 106
Reputation: 2738
$str = '14032017';
$strdate = DateTime::createFromFormat('dmY',$str)->format('Y-m-d H:i:s');
Use the correct format in your string. for 14032017 = dmY for 14-03-2017 = d-m-Y etc ...
Upvotes: 0
Reputation: 609
You can use the DateTime Class for this. ( i assume that your str is in dmY format )
$str = '14032017';
$date = DateTime::createFromFormat('dmY', $str);
echo $date->format('d/m/Y'); // Will print 14/03/2017
Upvotes: 3
Reputation: 3236
Try this
$str = '14032017';
$date = DateTime::createFromFormat('dmY', $str);
if($date === false){
echo 'Invalid Date';
}else{
echo $date->format('d-m-Y');
}
Upvotes: 5