Jump to content

Regional datepicker and strtotime()


facun

Recommended Posts

Hello,

 

I purchased a WP events plugin for a project and translated everything into Spanish, but I have a problem with unix timestamps as they're based on strtotime() which only accepts English strings. This is the function that generates the event timestamp:

private function save_timestamp() {

        $date_flag = false;
        $date = date_parse( $this->date );

        if( $date["error_count"] == 0 && checkdate( $date["month"], $date["day"], $date["year"] ) )
            $date_flag = true;


        if( $date_flag ) {
            $timestamp = strtotime( $this->date );
        }
        else {

            $event_unix = get_post_meta( $this->post_id, 'event-unix', true );

            if( ! empty( $event_unix ) )
                $timestamp = $event_unix;
            else
                $timestamp = current_time( 'timestamp' );
        }

        update_post_meta( $this->post_id, 'event-unix', $timestamp );

    }

So, being the datepicker in Spanish, events are getting current dates (event creation date).

The web is full of documentation about displaying unix dates in other languages, but I've not been able to find anything that does the opposite. I tried putting setlocale(LC_TIME, "es_ES") into the above function... I guess this is not the way to go.

 

This is what I'm using to translate the jquery datepicker (as you can see I did not event tried to change the dateformat, just the texts so users understand what they're doing):

$.datepicker.regional['es'] = {
        closeText: "Cerrar", // Display text for close link
        prevText: "Anterior", // Display text for previous month link
        nextText: "Siguiente", // Display text for next month link
        currentText: "Hoy", // Display text for current month link
        monthNames: ["Enero","Febrero","Marzo","Abril","Mayo","Junio",
            "Julio","Agosto","Septiembre","Octubre","Noviembre","Diciembre"], // Names of months for drop-down and formatting
        monthNamesShort: ["Ene", "Feb", "Mar", "Abr", "May", "Jun", "Jul", "Ago", "Sep", "Oct", "Nov", "Dic"], // For formatting
        dayNames: ["Domingo", "Lunes", "Martes", "Miercoles", "Jueves", "Viernes", "Sabado"], // For formatting
        dayNamesShort: ["Dom", "Lun", "Mar", "Mie", "Jue", "Vie", "Sab"], // For formatting
        dayNamesMin: ["Do","Lu","Ma","Mi","Ju","Vi","Sa"], // Column headings for days starting at Sunday
        //weekHeader: "Sm", // Column header for week of the year
        //dateFormat: "mm/dd/yy", // See format options on parseDate
        //firstDay: 1, // The first day of the week, Sun = 0, Mon = 1, ...
        //isRTL: false, // True if right-to-left language, false if left-to-right
        //showMonthAfterYear: false, // True if the year select precedes month, false for month then year
        //yearSuffix: "" // Additional text to append to the year in the month headers
    };
    $.datepicker.setDefaults($.datepicker.regional['es']);

Any help would be greatly appreciated.

 

Link to comment
Share on other sites

The date picker should not be creating dates with names. Regardless of what it displays to the user, it should always "output" in a standardized format like Y-m-d or m/d/Y.

 

But that's what it appears to be doing - at least according to the "dateFormat" (assuming that since it's not defined there then the date picker is using the default). So are you sure that the $this->date in the code is correct? It's not somehow being set to the current date?

Link to comment
Share on other sites

Hi, requinix. Thanks for the response.

 

My guess is that $this->date refers to this object:

/**
 * this global is only used for this template
 * profile-events-create.php
 */

global $pp_ec;
$pp_ec = new PP_Simple_Events_Create();

Then, in profile-events-create.php there's an input to set the event date:

<input type="text" id="event-date" name="event-date" placeholder="<?php echo __( 'Click to add Start Date...', 'bp-simple-events' ); ?>" value="<?php echo $pp_ec->date; ?>" />

And this is the correspondent js for the above input:

$('#event-date').datetimepicker({
        controlType: 'select',
        oneLine: true,        
        dateFormat: 'DD, MM d, yy'
});

Everything works as it should until you translate the date picker as posted above -> $.datepicker.setDefaults($.datepicker.regional['es']);

Link to comment
Share on other sites

I tried to convert Spanish date string into unix timestamp this way, but I guess I'm missing something:

private function save_timestamp() {

        $date_flag = false;
        setlocale(LC_ALL,"es_ES");
        $date = DateTime::createFromFormat("D, M d, Y g:ia", $this->date);

        if( $date["error_count"] == 0 && checkdate( $date["month"], $date["day"], $date["year"] ) )
            $date_flag = true;


        if( $date_flag ) {
            $timestamp = $date->getTimestamp();
        }
        else {

            $event_unix = get_post_meta( $this->post_id, 'event-unix', true );

            if( ! empty( $event_unix ) )
                $timestamp = $event_unix;
            else
                $timestamp = current_time( 'timestamp' );
        }

        update_post_meta( $this->post_id, 'event-unix', $timestamp );        

    }

$timestamp = $date->getTimestamp() may not be working.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.