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
date_default_timezone_set('UTC'); echo "<strong>Display current date dd/mm/yyyy format </strong>". "<br />"; echo date("d/m/Y"). "<br />"; echo "<strong>Display current date mm/dd/yyyy format</strong> "."<br />"; echo date("m/d/Y")."<br />"; echo "<strong>Display current date mm-dd-yyyy format </strong>".
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.
Try this
$birthdate= strtotime($_POST['brithday']);
$brithdateFormated = date("d/m/Y",$birthdate);
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With