php - WooCommerce Bookings - 在自定义日期格式上用 PHP 比较两个时间戳

标签 php wordpress date woocommerce timestamp

使用 WooCommerce Bookings plugin ,我正在开发一个系统,我必须用 PHP 比较两个日期。

我有下一个 sql 查询来获取这个日期:

 $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 给我们一个西类牙日期,例如 septiembre 2016,或 septiembre 1, 2016(2016 年 9 月,或 2016 年 9 月 1 日)例如。

我想比较一个日期和“今天”,所以我尝试使用这个 PHP 代码:

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

但它不起作用。

我怎样才能做到这一点?

谢谢

最佳答案

是的,这个自定义函数是否可行,我在关联数组中列出了西类牙月份,数字月份作为键,我重新排序日期以通过 strtotime() 函数输出它。该函数还可以返回当前时间,参数为'now'。

这是函数代码:

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 );
}

此代码位于您的事件子主题或主题的 function.php 文件中。

因为我真的不知道日期是像 septiembre 1, 2016 还是像 septiembre 1,2016(逗号后没有空格),您会在上面的代码中找到一个带注释的替代项。

我的代码也适用于 febrero, 2016febrero 2016 日期格式。

请检查我在 $month_arr 数组中的月份名称是否有任何错误......


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
}

关于php - WooCommerce Bookings - 在自定义日期格式上用 PHP 比较两个时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38940674/

相关文章:

windows - 验证返回码 : 20 when testing OpenSSL

wordpress - 在Wordpress中,您可以从评论ID中获取帖子ID吗

mysql - 由于日期格式,mysql 中的加载/导入失败

php - Carbon 返回错误的日期错误

json - 使用 Postman 将 JSON 请求对象中的日期对象设置为服务器

php - 使用 jQuery 将 JSON 发送到服务器

php - Javascript 将 PHP Json 转换为 javascript 数组

php - WordPress <?php the_content(); ?> 没有 "Read More"链接

php - 比较同一个表的不同列和行

javascript - 将集合数组添加到 Backbone JS 中先前添加的集合中