I want to convert date into d-M-y
format and it seems that i am doing something wrong. Kindly help me to correct it.
<?php
$date = '30/04/2017';
echo date('d-M-y', strtotime($date));
?>
My Output:
31-Dec-69
By I want output as 30-Apr-17
I want to convert date into d-M-y
format and it seems that i am doing something wrong. Kindly help me to correct it.
<?php
$date = '30/04/2017';
echo date('d-M-y', strtotime($date));
?>
My Output:
31-Dec-69
By I want output as 30-Apr-17
Use DateTime objects when you're working with dates and times. You can use DateTime::createFromFormat() to parse the date string and then the DateTime::format() to format it the way you want:
<?php
$str = '30/04/2017';
$date = DateTime::createFromFormat('d/m/Y', $str);
echo $date->format('d-M-Y');
?>
Use DateTime::createFromFormat()
$date = DateTime::createFromFormat('d/m/Y', '30/04/2017');
echo $date->format('d-M-Y');
<?php
$date = '30-04-2017';
echo date('d-M-y', strtotime($date));
?>
or use
<?php
$date = '25/05/2010';
$date = str_replace('/', '-', $date);
echo date('d-M-y', strtotime($date));
?>
Try this:
<?php
$date = '30/04/2017';
$newDate = str_replace('/', '-', $date);
echo date('d-M-y', strtotime($newDate));
?>
// Output: 30-Apr-17