11
$start_date = "2013-05-01";
$last_date = "2013-08-30";

我怎样才能得到这两个日期之间的星期二和星期四的日期?

4

9 回答 9

10
<?php
$start    = new DateTime('2013-05-01');
$end      = new DateTime('2013-08-30');
$interval = DateInterval::createFromDateString('1 day');
$period   = new DatePeriod($start, $interval, $end);

foreach ($period as $dt) {
    if ($dt->format("N") == 2 || $dt->format("N") == 4) {
        echo $dt->format("l Y-m-d") . "<br>\n";
    }
}

看到它在行动

这段代码的作用:

  1. 使用DateTime创建开始日期对象。
  2. 使用 DateTime 创建一个开始日期对象。
  3. 创建一个DateInterval对象来表示我们要迭代的时间间隔。在这种情况下 1 天。
  4. 创建一个DatePeriod对象来管理这些对象。
  5. 使用 DatePeriod,它遍历从开始日期开始到结束日期结束的日期。我们使用DateTime::format()withN参数来获取星期几。如果一周中的天数是2(星期二)或4(星期四),则回显它的值。
于 2013-05-08T12:26:37.453 回答
4

一些 PHP-Fu

$start_date = '2013-05-01';
$last_date = '2013-08-30';

$dates = range(strtotime($start_date), strtotime($last_date),86400);
$days = array('tuesday' => array(), 'thursday' => array());

array_map(function($v)use(&$days){
        if(date('D', $v) == 'Tue'){
            $days['tuesday'][] = date('Y-m-d', $v);
        }elseif(date('D', $v) == 'Thu'){
            $days['thursday'][] = date('Y-m-d', $v);
        }
    }, $dates); // Requires PHP 5.3+

print_r($days);

输出

Array
(
    [tuesday] => Array
        (
            [0] => 2013-05-07
            [1] => 2013-05-14
            [2] => 2013-05-21
            [3] => 2013-05-28
            [4] => 2013-06-04
            [5] => 2013-06-11
            [6] => 2013-06-18
            [7] => 2013-06-25
            [8] => 2013-07-02
            [9] => 2013-07-09
            [10] => 2013-07-16
            [11] => 2013-07-23
            [12] => 2013-07-30
            [13] => 2013-08-06
            [14] => 2013-08-13
            [15] => 2013-08-20
            [16] => 2013-08-27
        )

    [thursday] => Array
        (
            [0] => 2013-05-02
            [1] => 2013-05-09
            [2] => 2013-05-16
            [3] => 2013-05-23
            [4] => 2013-05-30
            [5] => 2013-06-06
            [6] => 2013-06-13
            [7] => 2013-06-20
            [8] => 2013-06-27
            [9] => 2013-07-04
            [10] => 2013-07-11
            [11] => 2013-07-18
            [12] => 2013-07-25
            [13] => 2013-08-01
            [14] => 2013-08-08
            [15] => 2013-08-15
            [16] => 2013-08-22
            [17] => 2013-08-29
        )

)

在线演示

于 2013-05-08T12:33:15.983 回答
2
$start_date = strtotime("2013-05-01");
$last_date = strtotime("2013-08-30");
while ($start_date <= $last_date) {
    $start_date = strtotime('+1 day', $start_date);
    if (date('N',$start_date) == 2 || date('N',$start_date) == 4){
        echo date('Y-m-d', $start_date).PHP_EOL;
    }
}
于 2013-05-08T12:27:52.733 回答
1

<?php echo date('Y-m-d', strtotime('next thursday', strtotime($start_date)));

当然也适用于周二

于 2013-05-08T12:26:53.687 回答
1

请为您的解决方案使用以下功能,

 function daycount($day, $startdate, $lastdate, $counter=0)
    {
        if($startdate >= $lastdate)
        {
            return $counter;
        }
        else
        {
            return daycount($day, strtotime("next ".$day, $startdate), ++$counter);
        }
    }

$start_date = "2013-05-01";
$last_date = "2013-08-30";

echo "Tuesday Count - ".daycount("tuesday", strtotime($start_date), strtotime($last_date));
echo "<br/>";
echo "Thursday Count - ".daycount("thursday", strtotime($start_date), strtotime($last_date));
于 2013-05-08T12:31:50.917 回答
0

试试这个

$startDate = strtotime($start_date);
$endDate = strtotime($last_date);
while ($startDate < $endDate) {
    echo date('Y-m-d', $startDate ). "\n";
    // Give the condition to find last Tuesday
    $startDate = strtotime( 'next Tuesday', $startDate );
}
于 2013-05-08T12:26:06.360 回答
0

使用日期时间:

$start_date = "2013-05-01";
$last_date = "2013-08-30";

$start = new DateTime($start_date);
$clone = clone $start;

$start->modify('next thursday');
$thursday=$start->format('Y-m-d');

$clone->modify('next tuesday');
$tuesday=$clone->format('Y-m-d');

echo $thursday; //2013-05-02
echo $tuesday; //2013-05-07

我们需要对象,因为 if in interval tuesdayis beforethursday我们将拥有 next tuesday。但是您可以修改少量代码以使用一个对象。

于 2013-05-08T12:35:41.107 回答
0

借助几个 php 日期函数,这可以轻松解决..

<?php

// Create the from and to date
$start_date = strtotime("2013-05-01");
$last_date  = strtotime("2013-08-30");

// Get the time interval to get the tue and Thurs days
$no_of_days = ($last_date - $start_date) / 86400; //the diff will be in timestamp hence dividing by timestamp for one day = 86400
$get_tue_thu_days = array();

// Loop upto the $no_of_days
for($i = 0; $i < $no_of_days; $i++) {
    $temp = date("D", $start_date);
    if($temp == "Tue" || $temp == "Thu") {
      $get_tue_thu_days[] = date("D/M/Y", $start_date); //formating date in Thu/May/2013 formate.
    }
    $start_date += 86400;
}

print_r($get_tue_thu_days);
于 2013-05-08T13:16:57.087 回答
-1

如果您有一个您知道是星期二/星期四的参考日期,您可以找到从参考日期起 7 天的倍数的天数,这些天将始终是一周中的同一天。

于 2013-05-08T12:24:36.730 回答