3

I am using this Bootstrap DatePicker: code below

<input class="datepicker" name="date">

<script> //date picker js
  $(document).ready(function() {  
      $('.datepicker').datepicker({
         todayHighlight: true,
         "autoclose": true,
      });
  });   
</script>

and I capture that in my PHP here:

$date = $_POST['date'];

The problem is that the DatePicker gives me the format dd/mm/yyyy when I need it yyyy-mm-dd in my $date variable. How do I reformat this?

Nate May
  • 3,814
  • 7
  • 33
  • 86

4 Answers4

3

I'm sure that you can set this in the date picker, but in PHP you can use:

$date = DateTime::createFromFormat("d-m-Y", $_POST['date'])->format('Y-m-d');

And from Bootstrap DatePicker documentation: http://bootstrap-datepicker.readthedocs.org/en/latest/options.html#format

Ron Dadon
  • 2,666
  • 1
  • 13
  • 27
  • Are you sure $_POST['date'] contain a value? And that is in the format of dd-mm-yyyy? You var_dump($_POST) to check your POST array. – Ron Dadon May 27 '15 at 22:43
1

You can easily adapt https://stackoverflow.com/a/2487938/747609 to suit your need. Something along this line should solve your problem:

$originalDate = $_POST['date'];
$newDate = date("Y-m-d", strtotime($originalDate));
Community
  • 1
  • 1
IROEGBU
  • 948
  • 16
  • 33
0

Try this page http://php.net/manual/en/datetime.createfromformat.php. Convert the string to a date object and output it as a formatted date

You want Y-m-d.

Connor Wyatt
  • 166
  • 11
  • This is tough to work with because my problem is that I do not know where to format the date, on the php side or the html side. – Nate May May 27 '15 at 22:34
  • Read about DateTime format options - you don't want YY-MM-DD, you want Y-m-d. Link: http://php.net/manual/en/datetime.createfromformat.php – Ron Dadon May 27 '15 at 22:44
  • Read the OP, he was wants it in this format yyyy-mm-dd with 4 digits, 2 digits, 2 digits. Yours will give yy-m-d where there will be no leading zeros. – Connor Wyatt May 28 '15 at 05:35
  • 1
    I quote from the PHP link from my last comment: "D and l - A textual representation of a day". So capital D is for sure not the solution."d and j -Day of the month, 2 digits with or without leading zeros" - so lower case are the solution. The same applies to the month. "Y - A full numeric representation of a year, 4 digits". So the format of "Y-m-d" in PHP's date functions or DateTime objects, is a 4 digit year-2 digit month-2 digit day. The page that you were looking at, is for "Localized Notations", and the symbols table is to explain the notations - not the formats for the format function. – Ron Dadon May 28 '15 at 13:12
  • Apologies, I've changed it to reflect the right answer – Connor Wyatt May 29 '15 at 14:11
0

****** SOLUTION ******

To solve this issue I saw that the variable 'date' was formatted dd/mm/yyyy and was a string. I was able to parse it and rebuild as a date in the proper format.

list($m,$d,$y) = explode("/",$_POST['date']);
$timestamp = mktime(0,0,0,$m,$d,$y);
$date = date("Y-m-d",$timestamp);

*Note - I also had to surround the $date variable in single quotes in the query:

$sql = "INSERT INTO tableName (..., date, ...)
        VALUES (..., '".$date."', ...)";
Nate May
  • 3,814
  • 7
  • 33
  • 86
  • A couple of things... you say the format is `dd/mm/yyyy`, but your code is treating the first part as month and 2nd as day (i.e. `mm/dd/yyyy`). You're best passing it through `strtotime` to do the conversion. Incidentally, PHP treats dates with slashes as UK format (`dd/mm/yyyy`), but dates with dashes as US format (`mm-dd-yyyy`). – Kevin Nagurski May 29 '15 at 14:18
  • I see your point. It's been too long to remember whether it was mm/dd/yyy instead (it may have been), but I do remember being quite careful since I'm trying to build my rep here. 'strtotime' wasn't working for me in this case but it did work with a timePicker string conversion. Sorry I couldn't clear that up. – Nate May Jun 03 '15 at 03:46