I set the jQuery Datepicker format to D d.m. displayed as Th 3.3. for a custom meta field that I want to use to sort posts. In SQL, the custom meta field is saved as D d.m.

I would like to display the D d.m. format on the front end and store it as mm-dd-yy or 03-03-2011 in my SQL database.

Any ideas?

My input field for the custom meta field cp_date:

 <input name="<?php echo $result->field_name; ?>" id="<?php echo $result->field_name; ?>" type="text" minlength="2" value="<?php if(isset($_POST[$result->field_name])) echo $_POST[$result->field_name]; ?>" autocomplete="off" class="datepicker <?php if($result->field_req) echo 'required' ?>" /> 
  • Voted to close. This isn't a WordPress question. I vote we move it to SO since it's more generally about date formatting and manipulation. Mar 3, 2011 at 18:07
  • @ John P Bloch... I need to build a custom post query in Wordpress that depends solely on that date format. So if it ain't the right forum, tell me where to go and ask the question.
    – Mia
    Mar 3, 2011 at 18:12

2 Answers 2


Firstly you'll need to stop storing the dates in D d.m format, the queries aren't going to be able to sort based on that data.

As wyrfel pointed out, you'll need to use the alternate field option to have two fields, one that shows the pretty(or your chosen) date format, and another that holds the value you store in the DB(in a format that the queries can sort on correctly, like yy-mm-dd).

Based on the one line of code you posted, give this a shot..

PHP/HTML: (to replace the code you posted)

$date_valid = false;
if( isset( $_POST[$result->field_name] ) ) {
    $date_parts = explode( '-', $_POST[$result->field_name] );
    $date_valid = ( 3 == count( $date_parts ) ) 
        // Validate date - checkdate returns true/false
        ? checkdate( $date_parts[1], $date_parts[2], $date_parts[0] ) 
        : false;
$display_date = ( $date_valid ) ? date( 'D d.m', strtotime( implode( '-', $date_parts ) ) ) : '';
$storage_date = ( $date_valid ) ? $_POST[$result->field_name] : '';
$req =  $result->field_req ? ' required' : '';
<input type="text" value="<?php echo $display_date; ?>" class="datepicker<?php echo $req ?>" />
<input name="<?php echo $result->field_name; ?>" type="hidden" value="<?php echo $storage_date; ?>" class="date_alternate" />

Datepicker jQuery: (just pull what you need from this)

jQuery(function($) {
    $( ".datepicker" ).datepicker({
        dateFormat: 'D d.m',
        altField: ".date_alternate",
        altFormat: "yy-mm-dd"

That way all your dates get stored in yy-mm-dd format, but the user sees the date in D d.m.

I tested this approach locally and was able to get the functionality needed, here's a few screenshots of my theme options page using a date field and with your chosen date format(my code was obviously a little different to what's above, but the approach was the same).

Selecting a date:
enter image description here Current values are at the bottom under the "Live Fetch" section.

What you see after clicking a date in the calendar:
enter image description here

Visual and stored values:
enter image description here

I thought it might just be nice to see the code put into practice and hope that helps.. :)

  • @ t31os... thank you so very much. It helps a great deal!!!
    – Mia
    Mar 5, 2011 at 9:19

please view the 'alternate Field' example and source code on this page. Make your alternative field a hidden one and then save the value of the alternate field rather than the visible one into your metadata.

Ahh...of course your date format for the alternate field should be 'yy-mm-dd'.

  • Thank you very much. After googleling for hours I found that same solution. Trying to make it work now. I am using this field for a front end form. Once the user fills out the form, the form results are displayed on the next page. And it shows the wrong date format. P.S. I tried to vote you up but I don't have enough points to do so.
    – Mia
    Mar 4, 2011 at 11:22
  • @Mia: Thanks. You can still vote me up later ;-) (if you haven't, yet). BTW...you could convert the date format in PHP as well, but i think above solution is easier.
    – wyrfel
    Mar 4, 2011 at 13:38
  • @ wyrfel Already did. I'm eligible to vote now. So you got my vote! Also, I finally managed to make it work. I will have to live with the wrong output format until I figure out how to do it. Thanks again.
    – Mia
    Mar 4, 2011 at 13:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.