PHP检查两个日期之间的日期

时间:2015-07-14 02:53:34

标签: javascript php mysql date time

我在php中有代码

date_default_timezone_set('Asia/Jakarta');
$dt         = new DateTime('first Saturday of this month');
$periode1   = $dt->format('Y-m-d');
$periode11  = date( "Y-m-d", strtotime( "$periode1 +6 day" ) );

$tgl_delivered  = "2015-07-10";
if (($tgl_delivered >= $periode1) && ($tgl_delivered <= $periode11)){
$tanggal_periode="$periode1 | $periode11";
}
echo "$tanggal_periode";

结果是“2015-08-08 | 2015-08-14”;

然而我希望这个结果是“2015-07-04 | 2015-07-10” 我的代码怎么了?

3 个答案:

答案 0 :(得分:0)

我不相信字符串转换。相反,我会计算该月第一天的工作日:

$day1=mktime(0,0,0,date('n'),1,date('Y');
$dow=date('w',$day1);

并弄清楚第一个星期六:

$sat = (6-$dow)*3600*24+$day1;

交货日相隔6天:

$delivery = $sat + 3600*24*6;

您可以尝试使用上述值的代码吗?

答案 1 :(得分:0)

您可以尝试使用此功能:

function checkBetweenDates($date, $start, $end) {
    $dateValue  = new DateTime($date);
    $startValue = new DateTime($start);
    $endValue   = new DateTime($end);

    return ($dateValue >= $startValue && $dateValue <= $endValue);
}

/* function return true or false */
var_dump(checkBetweenDates('first Saturday of this month', '2015-07-04', '2015-07-10'));

答案 2 :(得分:0)

试试此代码

function getTanggalPeriod($tglDelivered)
{
    $data=explode("-",$tglDelivered);
    $periode1 = date('Y-m-d',strtotime('First Saturday '.date('F o', @mktime(0,0,0, $data[1], 1, $data[0]))));
    $periode11 = date('Y-m-d',strtotime($periode1.' +6 day'));
    if (($tglDelivered >= $periode1) && ($tglDelivered <= $periode11))
    {
        echo $tanggal_periode="$periode1 | $periode11";
    }
    else
    {
        echo 'Not OK';
    }
} 
$tglDelivered = date('Y-m-d',strtotime("2015-07-10")); // Make sure of maintain the date format.
getTanggalPeriod($tglDelivered);