简体   繁体   中英

WooCommerce Bookings - Comparing two timestamps with PHP on custom dates format

Using WooCommerce Bookings plugin , I'm developing a system where I have to compare two dates with PHP.

I have the next sql query to get this date:

 $consulta2="SELECT meta_value FROM pwkynbjbwoocommerce_order_itemmeta WHERE  meta_key='Fecha de Reserva' AND order_item_id=".$row["order_item_id"];

Fecha de Reserva give us a spanish date like septiembre 2016 , or septiembre 1, 2016 (September 2016, or September 1,2016) for example.

I want to compare one date with "today", so I have tried to use this PHP code:

 if (strtotime($row2["meta_value"])>time()){}

But it doesn't work.

How can I achieve this?

Thanks

Yes is it possible with this custom function where I list in an associative array the spanish month with numerical month as key, and I reorder the date to output it through strtotime() function. This function can also return the current time with 'now' as parameter.

This is the function code:

function bk_date( $date ) {

    // Removing the coma (if there is a coma + a space)
    $my_time = str_replace ( ',', '', $date );
    // or replacing the coma by a space (if there is a coma alone without a space)
    // $my_time = str_replace ( ',', ' ', $the_date );

    $my_time_arr = explode( ' ', $my_time );
    $my_time_count = count( $my_time_arr );
    $month = $my_time_arr[0];
    if ( count( $my_time_arr ) > 2 ) { // When there is the month, the day and the year
        // Formating the day in 2 digits
        $day = $my_time_arr[1] < 10 ? '0'.$my_time_arr[1] : $my_time_arr[1];
        $year = $my_time_arr[2];
    } else { // When there is the month, the day and the year
        $year = $my_time_arr[1];
    }
    // Array of spanish month
    $month_arr = array('01' => 'enero', '02' => 'febrero', '03' => 'marzo', '04' => 'abril', '05' => 'mayo', '06' => 'junio', '07' => 'julio', '08' => 'agosto', '09' => 'septiembre', '10' => 'octubre', '11' => 'noviembre', '12' => 'diciembre');

    // Browse the list of month and compare (when $value match, it's replace by the index $key
    foreach ( $month_arr as $key => $value ) {
        if ( $month == $value ) {
            $month = $key;
            break;
        }
    }
    if ( count( $my_time_arr ) > 2 )
        $result = $year . '-' . $month . '-' . $day;
    else
        $result = $year . '-' . $month;

    return $date == 'now' ? strtotime( $date ) : strtotime( $result );
}

This code goes on function.php file of your active child theme or theme.

As I don't really know if the date is like septiembre 1, 2016 or like septiembre 1,2016 (without a space after the coma), you will find a commented alternative in the code above.

My code work also for febrero, 2016 or febrero 2016 date format.

Please check that I haven't make any mistakes in the month names located in $month_arr array…


Usage:

echo bk_date($row2["meta_value"]); // display the timestamp of your spanish date.

// Comparing 2 timestamps (bk_date('now') return the "current time").
if ( bk_date($row2["meta_value"]) > bk_date('now') ) {
    // do something
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM