2016-08-14 14 views
3

WooCommerce Bookings pluginを使用して、PHPと2つの日付を比較するシステムを開発しています。WooCommerce Bookings - カスタム日付形式のPHPと2つのタイムスタンプを比較する

$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月、または9月1,2016)などのスペイン語の日付を与えるために:

私はこの日付を取得するには、次のSQLクエリを持っています例。

は、私が「今日」と1つの日付を比較したいので、私はこのPHPコードを使用しようとしました:

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

をしかし、それは動作しません。

どうすればこの問題を解決できますか?

おかげ

+0

それはではないでしょうSQL側の日付を比較する方が有益でしょうか?それはあなたの条件に一致するものだけ、すべての行の往復とメモリを必要としません。 – ppeterka

+0

'var_dump($ row2 [" meta_value "])'から何を得るのですか? –

+0

私はこれを例としています:string(12) "febrero 2017"またはstring(18) "septiembre 1、2016" – Carl35

答えて

2

はい、それは私がキーとして数値月でスペイン語の月連想配列内のリストこのカスタム関数で可能である、と私はのstrtotime()関数を介して出力にそれを日付を並べ替えます。この関数は、 'now'をパラメータとして現在の時刻を返すこともできます。このコードは、あなたのアクティブな子テーマやテーマのfunction.phpファイルに行く

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

これは、関数のコードです。日付がseptiembre 1, 2016のようなまたは(昏睡の後にスペースなし)septiembre 1,2016のようなものである場合

私は本当に知らないとして、あなたは上記のコードでは、コメントの代替を見つけるでしょう。

febrero, 2016またはfebrero 2016日付フォーマットのための私のコードの作品は。

私は$month_arr配列にある月の名前のいずれかのミスをしていないことを確認してください...


用途:

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 
} 
関連する問題