0

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

Rushil K. Pachchigar
  • 1,175
  • 2
  • 18
  • 38
Abhishek
  • 938
  • 1
  • 12
  • 36

4 Answers4

2

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'); 
?>

Working Demo

Rushil K. Pachchigar
  • 1,175
  • 2
  • 18
  • 38
1

Use DateTime::createFromFormat()

$date = DateTime::createFromFormat('d/m/Y', '30/04/2017');
echo $date->format('d-M-Y');
Taron Saribekyan
  • 1,284
  • 7
  • 12
0
<?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));
?>
goto
  • 7,454
  • 10
  • 46
  • 54
Shubhranshu
  • 501
  • 3
  • 12
0

Try this:

<?php
$date = '30/04/2017';

$newDate = str_replace('/', '-', $date);

echo date('d-M-y', strtotime($newDate));

?>

// Output: 30-Apr-17

Working Example

Mayank Pandeyz
  • 24,624
  • 3
  • 35
  • 55