【问题标题】:php: exclude dates from a date rangephp:从日期范围中排除日期
【发布时间】:2013-04-14 11:37:03
【问题描述】:

我试图弄清楚如何从我设置的日期范围中排除某些日期。日期范围可以正常工作,如下所示:

<?php $newBegin = new DateTime('6/30/2010');
$newEnd = new DateTime('7/12/2010');
$newEnd = $newEnd->modify( '+1 day' );

$newDaterange = new DatePeriod($newBegin, new DateInterval('P1D'), $newEnd);

foreach($newDaterange as $newDate){
    echo $newDate->format("jnY") . " ";
} ?>

这样打印结果:

3062010 172010 272010 372010 472010 572010 672010 772010 872010 972010 1072010 1172010 1272010

但是客户需要从每个日期范围中排除某些日期,所以我最好像这样输入日期:7/2/2010 7/4/2010 8/4/2010 并将它们排除在日期范围之外。这是可能吗?我不想排除周末之类的,我可以这样做,只需输入一组日期并将它们从日期范围中排除。任何建议将不胜感激!


更新:

根据@hek2mgl 的要求,我添加了var_dump()get_field('test_select'));

var_dump(get_field('test_select'));

结果:

array(2) { [0]=> string(8) "7/2/2010" [1]=> string(8) "

完整代码(不工作):

$newBegin = DateTime::createFromFormat('n/j/Y', '6/30/2010');
$newEnd = DateTime::createFromFormat('n/j/Y', '7/12/2010');
$newEnd = $newEnd->modify( '+1 day' );

$exclude = array();

// stores dates like so: 7/2/2010 7/3/2010
foreach(get_field('test_select') as $datestring) {
    $exclude []= new DateTime($datestring);
}

$newDaterange = new DatePeriod($newBegin, new DateInterval('P1D'), $newEnd);

foreach($newDaterange as $newDate){
    if(!in_array($newDate, $exclude)) {
        echo $newDate->format("jnY") . " ";
    }   
}

【问题讨论】:

  • 一条建议——不要使用可能不明确的日期格式。 7/12/2010 对你来说可能是 7 月 12 日,但对我来说这意味着 12 月 7 日。并且要注意不明确的输入会导致 PHP 假设错误的值。如果可能,请始终在代码中使用明确的日期格式。如果不可能,请使用DateTime::CreateFromFormat(),并明确指定格式。

标签: php html date datepicker


【解决方案1】:

无法使用DatePeriod 类排除某个范围内的多个日期。但是您可以将in_array()DateTime 对象一起使用。这可能会导致这样的代码:

$newBegin = new DateTime('6/30/2010');
$newEnd = new DateTime('7/12/2010');
$newEnd = $newEnd->modify( '+1 day' );

$exclude = array(
    new DateTime('7/2/2010'),
    new DateTime('7/4/2010'),
    new DateTime('8/4/2010')
);

$newDaterange = new DatePeriod($newBegin, new DateInterval('P1D'), $newEnd);

foreach($newDaterange as $newDate){
    if(!in_array($newDate, $exclude)) {
        echo $newDate->format("jnY") . " ";
    }   
} 

输出:

3062010 172010 372010 572010 672010 772010 872010 972010 1072010 1172010 1272010

更新:

在 cmets 中,您询问了如何将传入的日期字符串列表转换为可在 $exclude 数组中使用的 DateTime 对象。

例子:

$exclude = array();

// stores dates like so: 7/2/2010 7/3/2010
foreach(get_field('test_select') as $datestring) {
    $exclude []= new DateTime::createFromFormat('n/j/Y', $datestring);
}

就是这样。 :)

【讨论】:

  • 非常感谢!这几乎是完美的,只有一件小事,因为排除的日期(在数组中)将由用户在前端输入,而不是像上面的示例那样在后端输入,new DateTime('7/2/2010'), 是否可能是 @ 987654331@ 与用户在test_select 字段中输入的日期?
  • 如果test_select 包含由DateTime 解析的日期字符串,请使用new DateTime(get_field('test_select'));
  • 不错的,成功了!我已经稍微调整了一下,我保证的最后一个问题,test_select 字段现在打印一个日期数组。例如echo implode(' ', get_field('test_select')); 显示日期如下:7/2/2010 7/3/2010 是否可以将这些从日期范围中排除?我对 php 还是很陌生,所以我不是 100% 确定它会如何写...
  • 不完全是,get_field('test select') 返回空白,因此没有排除任何内容。我正在使用这个选择字段:advancedcustomfields.com/resources/field-types/select 来选择一个数组。因此echo implode(' ', get_field('test_select')); 返回7/2/2010 7/3/2010。有什么方法可以将它与您的示例结合起来吗? p.s.谢谢你的帮助顺便说一句:)
  • 您可以编辑您的问题并添加来自var_dump(get_field('test_select')); 的输出吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-10
  • 2015-12-30
  • 2013-03-01
  • 1970-01-01
相关资源
最近更新 更多