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,263
  • 2
  • 21
  • 40
Abhishek
  • 1,008
  • 1
  • 16
  • 39

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,263
  • 2
  • 21
  • 40
1

Use DateTime::createFromFormat()

$date = DateTime::createFromFormat('d/m/Y', '30/04/2017');
echo $date->format('d-M-Y');
Taron Saribekyan
  • 1,360
  • 7
  • 13
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,908
  • 10
  • 48
  • 58
Shubhranshu
  • 511
  • 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
  • 25,704
  • 4
  • 40
  • 59