PHP Function Reference

PHP DateTime - setISODate() Method



The PHP DateTime::setISODate() method sets the date of a DateTime object, according to the ISO 8601 standard - using weeks and day offsets rather than specific dates. The date_isodate_set() function is an alias of this method.

Syntax

//Object-oriented style
public DateTime::setISODate(year, week, dayOfWeek)

//Procedural style
date_isodate_set(object, year, week, dayOfWeek)

Parameters

object Required. For procedural style only: A DateTime object returned by date_create().
year Required. Specify an integer value representing year of the date.
week Required. Specify an integer value representing week of the date.
dayOfWeek Optional. Specify an integer value representing the day of the week. Default is 1.

Return Value

Returns the DateTime object with modified date on success, otherwise returns false.

Example: using both styles

The example below shows the usage of DateTime::setISODate() method.

<?php
  //creating a DateTime object
  $date = new DateTime();

  //setting date using Object-oriented style
  $date->setISODate(2015, 10, 2); 

  //formatting the datetime to print it
  echo $date->format("d-M-Y")."\n";

  //setting date using Procedural style
  date_isodate_set($date, 2015, 11, 2); 

  //formatting the datetime to print it
  echo $date->format("d-M-Y")."\n";
?>

The output of the above code will be:

03-Mar-2015
10-Mar-2015

Example: Modifying date of a DateTime object

This method can be used to modify date of the DateTime object to a new value. Consider the example below:

<?php
  //datetime string
  $datetime_string = "14-May-2015";

  //creating a DateTime object
  $date = new DateTime($datetime_string);

  //formatting the datetime to print it
  echo "Original date: ".$date->format("d-M-Y")."\n";

  //setting date to a new value
  $date->setISODate(2016, 10, 4); 

  //formatting the datetime to print it
  echo "Modified date: ".$date->format("d-M-Y")."\n";
?>

The output of the above code will be:

Original date: 14-May-2015
Modified date: 10-Mar-2016

Example: Adding values exceeding ranges

When values exceeding ranges are passed to this method, they will be added to their parent values. Consider the example below:

<?php
  //creating a DateTime object
  $date = new DateTime();

  //setting date to a new value
  $date->setISODate(2001, 10, 2);
  echo $date->format('d-M-Y') . "\n";

  //setting date with exceeding range of dayOfWeek -
  //exceeding days (2 days) will be added 
  $date->setISODate(2001, 10, 9);
  echo $date->format('d-M-Y') . "\n";

  //setting date with exceeding range of week -
  //exceeding weeks (3 weeks) will be added
  $date->setISODate(2001, 55, 2);
  echo $date->format('d-M-Y') . "\n";
?>

The output of the above code will be:

06-Mar-2001
13-Mar-2001
15-Jan-2002

Example: Finding the month a week is in

The example below illustrates on finding the month a week is in.

<?php
  //creating a DateTime object
  $date = new DateTime();

  //setting date to a new value
  $date->setISODate(2001, 10);

  //finding the month the week is in
  echo $date->format('n') . "\n";
?>

The output of the above code will be:

3

❮ PHP Date and Time Reference