W3docs

date_isodate_set()

The date_iso_set() function has the following syntax:

Syntax

The DateTime::setISODate() method has the following syntax:

The PHP DateTime::setISODate() method syntax

$date->setISODate(int $year, int $week, int $dayOfWeek)

where $date is a DateTime object, $year is the ISO year, $week is the week number (1–53), and $dayOfWeek is the day of the week (1 for Monday, 7 for Sunday).

Usage

The DateTime::setISODate() method sets the date of a DateTime object according to the ISO 8601 week-based calendar. Unlike standard calendar dates, ISO 8601 defines dates by year, week number, and day of the week. This is particularly useful for business, scheduling, or international standards where week-based calculations are required.

The DateTime::setISODate() method is useful when you need to set the date of a DateTime object to a specific week-based date. You can use it to set the date to any valid ISO week and day combination.

Examples

Here are some examples of how to use the DateTime::setISODate() method:

How to use PHP DateTime::setISODate()?

<?php
$date = new DateTime();
$date->setISODate(2023, 10, 3);
echo $date->format('Y-m-d\TH:i:s\Z');
?>

In this example, we create a new DateTime object and set its date to the 3rd day (Wednesday) of the 10th week of 2023. We then use the format() method to output the date in ISO 8601 format.

Example of PHP DateTime::setISODate()

<?php
$date = new DateTime();
$date->setISODate(2022, 52, 7);
echo $date->format('Y-m-d\TH:i:s\Z');
?>

In this example, we set the date of the DateTime object to the 7th day (Sunday) of the 52nd week of 2022.

How to use DateTime::setISODate() method in PHP?

<?php
$date = new DateTime();
$date->setISODate(2023, 24, 1);
echo $date->format('Y-m-d\TH:i:s\Z');
?>

In this example, we set the date of the DateTime object to the 1st day (Monday) of the 24th week of 2023.

Conclusion

In conclusion, the DateTime::setISODate() method is a useful tool when you need to set the date of a DateTime object to a specific week-based date. It follows the ISO 8601 standard, which is an international standard for date and time formats. With this guide, you can use the DateTime::setISODate() method with confidence and improve your PHP programming skills.

Practice

Practice

What does the ISODate::set method in PHP do?