获取上个月的下一个五个星期六日期

时间:2018-04-07 05:18:54

标签: php php-7 php-5.3 php-5.6 php-7.2

我需要从上个月的下周五开始。 例如。当前日期是07-04-2018所以我需要上个月的5个星期六。 输出03-03-18,10-03-18,17-03-18,24-03-18,31-03-18。 我可以通过使用下面的方式获得它,但这是完全硬编码的。 我如何在这里申请循环以获得上个月的下周五?

$firstSat= date("dmy", strtotime("first saturday of last month"));
$secondSat= date("dmy", strtotime("second saturday of last month"));
$thirdSat= date("dmy", strtotime("third saturday of last month"));
$forthSat= date("dmy", strtotime("fourth saturday of last month"));
$fifthSat= date("dmy", strtotime("fifth saturday of last month"));

1 个答案:

答案 0 :(得分:1)

您可以使用 PictureBox[] bossHealth = new PictureBox[20]; for( int i = 0; i<19; i++) { bossHealth[i].Name = "health"; bossHealth[i].Size = new Size(10, 26); bossHealth[i].BackColor = Color.LimeGreen; bossHealth[i].Location = new Point(this.Width / 2 + (i * 10), 12); bossHealth.Add(bossHealth[i]); Controls.Add(bossHealth[i]); }

new DateTime()

这将导致:

$y = 2018;
$m = 3;

$sat1 = new DateTime("first saturday of $y-$m");
$sat2 = new DateTime("second saturday of $y-$m");
$sat3 = new DateTime("third saturday of $y-$m");
$sat4 = new DateTime("fourth saturday of $y-$m");
$sat5 = new DateTime("fifth saturday of $y-$m");

echo $sat1->format('dmy') . "<br />";
echo $sat2->format('dmy') . "<br />";
echo $sat3->format('dmy') . "<br />";
echo $sat4->format('dmy') . "<br />";
echo $sat5->format('dmy') . "<br />";

获得一个月的所有星期六

030318
100318
170318
240318
310318

这将导致:

$date = "03/17/18"; //Date on mm/dd/yy format

$start = new DateTime($date);
$end = new DateTime($date);
$end = $end->add(new DateInterval('P40D'));

$satPeriod = new DatePeriod (
    $start,
    DateInterval::createFromDateString('next saturday'),
    $end
);

foreach ($satPeriod as $saturday) {
    echo $saturday->format("dmy");
    echo "<br />";
}