\WP_Stager_Integration\Calendar::calculate_offset_timestamp(int $timestamp, string $offset): int

Description

Add an ISO-8601 DateInterval spec offset to a local instant safely (DST aware).

Make sure to add a minus symbol '-' before the offset to make it negative, e.g. '-P1D'.

Parameters:

ParameterTypeDescription
$timestamp int The timestamp
$offset string The offset to apply. Examples: 'P1D', 'P1W', 'P1M'

Returns:

int The offset timestamp

Information

Fileclass-calendar.php line 590

Full Code

/**
 * Add an ISO-8601 <code>DateInterval</code> spec offset to a local instant safely (DST aware).
 *
 * Make sure to add a minus symbol <code>'-'</code> before the offset to make it negative, e.g. <code>'-P1D'</code>.
 *
 * @param int       $timestamp  The timestamp
 * @param string    $offset     The offset to apply. Examples: <code>'P1D'</code>, <code>'P1W'</code>, <code>'P1M'</code>
 *
 * @return int  The offset timestamp
 */
public static function calculate_offset_timestamp( $timestamp, $offset ) {

    // Convert to datetime
    $date_time = self::get_datetime_from_timestamp( $timestamp );

    // Check if the offset was passed as a negative offset
    if ( substr( $offset, 0, 1 ) === '-' ) {

        // Remove '-' from the offset
        $offset = str_replace( '-', '', $offset );

        // Subtract interval
        $date_time = $date_time->sub( new \DateInterval( $offset ) );
    } else {

        // Add interval
        $date_time = $date_time->add( new \DateInterval( $offset ) );
    }


    // Return timestamp
    return $date_time->getTimestamp();
}

💡 If you ever get stuck or have a question, please check our FAQs, our Free Integration Service, our paid Full Integration Service, or reach out to us!

Get WP Stager Integration

🎁 Limited offer: Use code WELCOME26 to get your first month for free!