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 回答