How to add N weeks to a given date in PHP

3 Answers

0 votes
$date = '2022/03/18';
$weeks = 3;

$dateTime = DateTime::createFromFormat('Y/m/d', $date);
$dateTime->add(DateInterval::createFromDateString($weeks . ' weeks'));

$date = $dateTime->format('Y-m-d');

echo $date;




/*
run:

2022-04-08

*/

 



answered Mar 22, 2022 by avibootz
0 votes
/**
 * Adds a specified number of weeks to a given date.
 *
 * @param DateTimeImmutable|string $date  The starting date (string or DateTimeImmutable).
 * @param int                      $weeks The number of weeks to add 
 *                                        (supports negative numbers to subtract).
 * @return DateTimeImmutable              The resulting date as an immutable object.
 * @throws Exception                      If the provided date string is invalid.
 */
function addWeeksToDate(DateTimeImmutable|string $date, int $weeks): DateTimeImmutable 
{
    // If the input is a string, convert it into a DateTimeImmutable object
    $dateTime = is_string($date) ? new DateTimeImmutable($date) : $date;
    
    // Construct an ISO 8601 duration interval string for weeks (e.g., "P2W" for 2 weeks)
    $interval = new DateInterval("P{$weeks}W");
    
    // Add the interval and return a new DateTimeImmutable instance
    return $dateTime->add($interval);
}

// ==========================================
// Example Usage:
// ==========================================

try {
    $startDate = '2026-06-01';
    $weeksToAdd = 4;

    $newDate = addWeeksToDate($startDate, $weeksToAdd);

    // Output the formatted result
    echo "Starting Date: " . $startDate . "\n";
    echo "After adding {$weeksToAdd} weeks: " . $newDate->format('Y-m-d') . "\n"; 
    // Output: 2026-06-29

} catch (Exception $e) {
    echo "Error parsing date: " . $e->getMessage();
}


/*
run:

Starting Date: 2026-06-01
After adding 4 weeks: 2026-06-29

*/

 



answered 1 day ago by avibootz
0 votes
/**
 * Adds a specified number of weeks to a date string formatted as 'Y/m/d'.
 *
 * @param string $dateStr The starting date (e.g., '2022/03/18').
 * @param int    $weeks   The number of weeks to add.
 * @return string         The resulting date formatted as 'Y-m-d'.
 * @throws Exception      If the date string cannot be parsed.
 */
function addWeeksToFormat(string $dateStr, int $weeks): string
{
    // Parse the date using the specific Y/m/d format (immutable version)
    $dateTime = DateTimeImmutable::createFromFormat('Y/m/d', $dateStr);
    
    // DateTimeImmutable::createFromFormat returns false on failure
    if ($dateTime === false) {
        throw new Exception("Invalid date format provided: '{$dateStr}'");
    }
    
    // Add weeks using a relative date string interval
    $newDateTime = $dateTime->add(DateInterval::createFromDateString("{$weeks} weeks"));
    
    return $newDateTime->format('Y-m-d');
}

// ==========================================
// Example Usage:
// ==========================================

$date = '2022/03/18';
$weeks = 3;

try {
    $result = addWeeksToFormat($date, $weeks);
    echo $result; // Output: 2022-04-08
} catch (Exception $e) {
    echo "Error: " . $e->getMessage();
}


/*
run:

2022-04-08

*/

 



answered 1 day ago by avibootz
...