Jump to content

Changing the format of a date in $_POST


mdemetri2

Recommended Posts

Hi

I have a javascript calendar that populates a form field (survey) in the format of dd/mm/yyyy. I don't know of a method to change the format in the calendar. 

 

So, I would like the format to be changed at the point the form is submitted and the date is used to update a record. 

 

I have tried to amend the portion of the update script for the survey field to change the format as part of it submitting the change. I used this line: 

 

GetSQLValueString(date("Y-m-d, strtotime("$_POST['survey']), "text")

 

but I'm not getting the expected results, essentially from the text field entry format of dd/mm/yyyy I want Y-m-d

 

Is this possible?

 

Thanks in advance. 

 

 

Link to comment
https://forums.phpfreaks.com/topic/288478-changing-the-format-of-a-date-in-_post/
Share on other sites

Look it up in the documentation for your particular datepicker. If there is none, use one that has.

 

http://api.jqueryui.com/datepicker/#option-dateFormat

 

PS: I prefer to set datepickers to use "31 Jan 2014" format as it is clear to the user, unambiguous and recognised by strtotime()

If changing the js code doesn't work, you can always reformat it in PHP

$calendar_date = '17/05/14';
$dt = DateTime::createFromFormat('d/m/y', $calendar_date);
$formatted_date = $dt->format('Y-m-d');

echo $formatted_date;       //--> 2014-05-17

Well, saying that you couldn't implement this is rather vague. What exactly is the problem?

 

Using DateTime to convert the format is a good solution and works perfectly fine. If you're not getting the expected result, you've made a mistake. But of course we can't help you with that unless you post your code and describe the specific problem.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.