You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在PHP中获取当月第一周周六日期?双输入框需求实现

Alright, let's work through this problem to get the exact dates you need. Here's a straightforward implementation using PHP:

Step 1: Calculate the First Saturday of the Current Month

First, we need to find the first Saturday in the current month. Here's how to do that:

// Get the first day of the current month in Y-m-d format
$firstDayOfMonth = date('Y-m-01');
// Calculate how many days we need to add to reach the first Saturday
// date('w') returns 0 (Sunday) to 6 (Saturday)
$daysToSaturday = (6 - date('w', strtotime($firstDayOfMonth))) % 7;
// Compute the first Saturday date
$firstSaturday = date('m/d/Y', strtotime($firstDayOfMonth . " +{$daysToSaturday} days"));

This logic works by checking what day of the week the first of the month is, then calculating the days needed to jump to the next (or current) Saturday.

Step 2: Calculate the Next Wednesday After That Saturday

Next, we need the Wednesday that comes right after this Saturday. That's exactly 4 days later, and strtotime will automatically handle any month transitions if the Saturday falls on the last day of the month:

$nextWednesday = date('m/d/Y', strtotime($firstSaturday . ' +4 days'));

Step 3: Update Your Input Fields

I also fixed the duplicate id attributes (they have to be unique in HTML) and corrected the value output (your original code was putting the PHP function string directly into the input instead of executing it). Here's the full working code:

<?php
// Calculate first Saturday of the current month
$firstDayOfMonth = date('Y-m-01');
$daysToSaturday = (6 - date('w', strtotime($firstDayOfMonth))) % 7;
$firstSaturday = date('m/d/Y', strtotime($firstDayOfMonth . " +{$daysToSaturday} days"));

// Calculate next Wednesday after the first Saturday
$nextWednesday = date('m/d/Y', strtotime($firstSaturday . ' +4 days'));
?>

<!-- First input: First Saturday of the month -->
<input id="start_date" name="start_date" placeholder="" type="text" class="form-control" value="<?php echo $firstSaturday; ?>" readonly="readonly" />

<!-- Second input: Next Wednesday after the first Saturday -->
<input id="end_date" name="end_date" placeholder="" type="text" class="form-control" value="<?php echo $nextWednesday; ?>" readonly="readonly" />

Test Scenarios to Verify

  • October 2024: First day is Tuesday (10/01), so first Saturday is 10/05. Next Wednesday is 10/09.
  • January 2025: First day is Wednesday (01/01), so first Saturday is 01/04. Next Wednesday is 01/08.
  • Edge Case (Month-End Saturday): November 2024 ends on Saturday (11/30). The next Wednesday would be 12/04, which strtotime handles automatically.

内容的提问来源于stack exchange,提问作者Movies Dhamaka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 16:22:43