Reputation: 411
I have date in this type of format: April 1st 2017
and I want to convert it into this type of format: 2017/04/01
in my CodeIgniter
code using php
. I have used below posted piece of code but it is not working. Please solve the issue.
Code:
$date = DateTime::createFromFormat('m/d/Y', "April 1st 2017");
echo "Date = ".$date->format('Y-m-d');
Upvotes: 1
Views: 735
Reputation: 4033
you can try this also
<?php
$date='22 march 2018';
echo date('m/d/Y', strtotime($date));
?>
Upvotes: 0
Reputation: 1
The format you specified for your date is incorrect.
It would convert '04/01/2017' but it does not suit April 1st 2017.
Try instead: createFromFormat('F dS Y')
Explanation:
F
- full textual representation of a month, such as January.
d
- day
S
- English ordinal suffix for the day of the month
Y
- 4-digit representation of year
Upvotes: 0
Reputation: 9373
You can use strtotime() and date() php functions as
$newDate = date("m/d/Y", strtotime("April 1st 2017"));
Or in CodeIgniter
$date = DateTime::createFromFormat('j F Y - H:i', 'April 1st 2017');
echo $date->format('m/d/Y H:i:s');
Upvotes: 1
Reputation: 19780
Your format can be used in the constructor of DateTime
. See accepted formats.
$date = new DateTime("April 1st 2017");
echo "Date = ".$date->format('Y-m-d');
Outputs:
Date = 2017-04-01
If you want to use DateTime::createFromFormat()
, you have to use the proper format
"F jS Y"
Upvotes: 0