I've the date format 12/May/2018. I need to change it to 2018-12-05.Please help me.
$originalDate = "12/May/2018";
$newDate = date("Y-m-d", strtotime($originalDate));
echo $newDate;
Output: 1969-12-31
Thanks in advance.
Use this code :
$originalDate = "12/May/2018";
$originalDate =str_replace("/", " ", $originalDate);
$newDate = date("Y-m-d", strtotime($originalDate));
echo $newDate;
There's no need for any manual parsing.
You should use PHP's DateTime class, which is much more flexible than the old date()
-function.
Here's an example that does what you want:
$date = DateTime::createFromFormat('d/M/Y', '12/May/2018');
echo $date->format('Y-m-d');
Demo: https://3v4l.org/Y5HF0
That date format is not supported by strtotime.
But replacing the /
with space is a supported format.
$originalDate = "12/May/2018";
$newDate = date("Y-m-d", strtotime(str_replace("/", " " ,$originalDate)));
echo $newDate;
The reason you get 1969-12-31 is because UNIX 0 is 1970-01-01 00:00:00.
When strtotime can't parse the date it returns false
.
False can be typeconverted to 0
.
That means date reads it as "Ymd" at UNIX time 0 (1970), and because of your timezone you get 1969-12-31.
try this,
$originalDate = "12/May/2018";
$newDate = date_format(date_create_from_format("j/F/Y", $originalDate), 'Y-m-d');
echo $newDate;
Note : if your day format is 01
use d
, if 1
use j
The output is: 2018-05-12
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.