2

関数から生成された複数の値を配列に挿入しようとすると問題が発生します。文字列を使用して関数を出力し、結果を手動でコピーすると機能しますが、文字列を配列に使用して機能させようとすると機能しません。

<?php 

function dateRange( $first, $last, $step = '+1 day', $format = 'm/d/Y' ) {

$current = strtotime( $first );
$last = strtotime( $last );

while( $current <= $last ) {

    $dates .= "'" . date( $format, $current) . "', ";
    $current = strtotime( $step, $current );
}

return $dates;
} 

$all_dates = dateRange( '01/20/1999', '01/23/1999'); 

echo $all_dates; /* PRINTS ALL DATES BETWEEN TWO DATES: '01/20/1999', '01/21/1999', '01/22/1999', '01/23/1999', */

query_posts( array(
'post_type' => 'bbdd',
'meta_query' => array(
    $location,
    array(
        'key' => 'date',
        'value' => array($all_dates), /*  DOESN'T WORK. INSTEAD, IF WE COPY THE RESULT OF "echo $all_dates;" MANUALLY, IT DOES WORK */
    ),
)
) );

?>
4

2 に答える 2

3

関数で配列ではなく文字列を返しています。

function dateRange( $first, $last, $step = '+1 day', $format = 'm/d/Y' ) {

    $current = strtotime( $first );
    $last = strtotime( $last );

    while( $current <= $last ) {

        $dates[] = date($format, $current);
        $current = strtotime($step, $current );
    }

    return $dates;
}

それは配列を返します。

次に、mysql クエリで次のようにします。

'value'   => $all_dates
于 2013-01-24T12:47:12.607 に答える
0

そもそも配列に入れてみませんか:

<?php

function dateRange( $first, $last, $step = '+1 day', $format = 'm/d/Y' ) {
    $dates = array();
    $current = strtotime( $first );
    $last = strtotime( $last );

    while( $current <= $last ) {

            $dates[] = date($format, $current);
            $current = strtotime( $step, $current );
    }

    return $dates;
} 

?>
于 2013-01-24T12:48:35.523 に答える