Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Format dd/mm/yy doesn't work #34

Closed
BIOSTALL opened this issue Nov 6, 2013 · 5 comments
Closed

Format dd/mm/yy doesn't work #34

BIOSTALL opened this issue Nov 6, 2013 · 5 comments

Comments

@BIOSTALL
Copy link

BIOSTALL commented Nov 6, 2013

I'm using the date format dd/mm/yy, however after saving the date either disappeared or came back in the wrong format. For example, saving 06/11/2013 would come back as 11/06/2013.

The problem is because the plugin uses the strtotime() PHP function which assumes the day and month be in a specific order. As a result I changed the update_value() function in file date_time_picker-v4.php as follows:

    function update_value( $value, $post_id, $field ) {
        $field = array_merge($this->defaults, $field);
        if ($value != '' && $field['save_as_timestamp'] == 'true') {
            if ($field['date_format'] == 'dd/mm/yy')
            {
                $value = str_replace('/', '-', $value);
            }
            $value = strtotime( $value );
        }

        return $value;
    }

Otherwise, great plugin :) Thanks.

@soderlind
Copy link
Owner

I've been a bit reluctant to add dd/mm/yy as a valid time format since it's not a "valid" format, but .. ok, I'll add it to the next release. Thank you for the code sample.

@squarestar
Copy link

I'm curious what is meant by 'dd/mm/yy ...[is] not a "valid" format'. Is this a coding issue or a convention issue? If conventional, dd/mm/yy is standard in Europe.

@BIOSTALL
Copy link
Author

The format 'dd/mm/yy' is standard in the UK and this plugin didn't work when using that format. I'm not sure about the default format in other countries.

@soderlind
Copy link
Owner

By "not valid" I mean that strtotime doesn't understand dd/mm/yy (see notes: "if the separator is a slash (/), then the American m/d/y is assumed").

If the requirements for running WordPress were PHP 5.3 I would have used DateTime::createFromFormat, but it's PHP 5.2.4 and hence I have to use strtotime .

@soderlind
Copy link
Owner

Fixed, please test the latest version here at github

@BIOSTALL, I did this (to support d/m/y (i.e. a single 'd'), there are some countries using it:

function update_value( $value, $post_id, $field ) {
    $field = array_merge($this->defaults, $field);
    if ($value != '' && $field['save_as_timestamp'] == 'true') {
        if (preg_match('/^dd?\//',$field['date_format'] )) { //if start with dd/ or d/ (not supported by strtotime())
            $value = str_replace('/', '-', $value);
        }
        $value = strtotime( $value );
    }

    return $value;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants