7

I want to keep date format d/m/Y (24/12/2013) in client side (users enter date in that format from JQuery date picker). But in server side I convert it to Y-m-d(2013-12-24) format.

To do that I wrote code in this way

$brithdate = explode('/', $_POST['brithday']);
$brithdateFormated = $brithdate[2] . "-" . $brithdate[1] . "-" . $brithdate[0];

Is this correct? or is there any easy way to do that

Gayan
  • 2,845
  • 7
  • 33
  • 60
  • 1
    take a look at [strtotime](http://de3.php.net/manual/en/function.strtotime.php) after strtotime use [date](http://de2.php.net/manual/en/function.date.php) to format it again – demonking Jan 01 '14 at 12:10
  • 1
    Duplicate of [Convert one date format into another in PHP](http://stackoverflow.com/questions/2167916/convert-one-date-format-into-another-in-php) and thousands of other questions – Mark Baker Jan 01 '14 at 12:20

3 Answers3

27

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:

$str = '24/12/2013';
$date = DateTime::createFromFormat('d/m/Y', $str);
echo $date->format('Y-m-d'); // => 2013-12-24

For a list of available formatting options, see the documentation.

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
3

Try this

$birthdate= strtotime($_POST['brithday']);
$brithdateFormated = date("d/m/Y",$birthdate);
Rizwan Sultan
  • 187
  • 10
1

This is working for me.

$start_date='04/05/2018';
$convertedDate= DateTime::createFromFormat('d/m/Y', $start_date)->format('Y-m-d');

Note: The First parameter should be same as your $start_date with '/' also. If you put '-' there, then it will not perform the correct conversion.

Parth Patel
  • 859
  • 2
  • 11
  • 28