• PHP Video Tutorials

PHP date_isodate_set() Function


Advertisements

Definition and Usage

The date_isodate_set() function is an alias of DateTime::setISODate(). Using this function you can set an ISO date. This uses weeks and days in a week.

Syntax

date_isodate_set($object, $year, $week, $day)

Parameters

Sr.No Parameter & Description
1

object(Mandatory)

This is a DateTime object to which you need to set the date.

2

year(Mandatory)

This is an integer value representing the year of a date.

3

week(Mandatory)

This is an integer value representing the week of a month.

4

day(Mandatory)

This is an integer value representing the day the week.

Return Values

PHP date_isodate_set() function returns the DateTime object with modified value. Incase of failure, this function returns the boolean value false.

PHP Version

This function was first introduced in PHP Version 5.2.0 and, works with all the later versions.

Example

Following example demonstrates the usage of the date_isodate_set function −

Live Demo
<?php
   //Creating a date
   $date = new DateTime();
   //Setting the ISO date
   date_isodate_set($date, 2019, 03, 3);   
   print("Date: ".date_format($date, "Y/m/d"));
?>

This will produce following result −

Date: 2019/01/16

Example

Following example creates a DateTime object and modifies its date using the date_isodate_set() function. −

<?php
   //Date string
   $date_string = "25-09-1989";
   //Creating a DateTime object
   $date_time_Obj = date_create($date_string);
   print("Original Date: ".date_format($date_time_Obj, "Y/m/d"));
   print("\n");
   //Setting the date
   $date = date_isodate_set($date_time_Obj, 2015, 4, 3 );   
   print("Modified Date: ".date_format($date, "Y/m/d"));
?>

This will produce following result −

Original Date: 1989/09/25
Modified Date: 2015/01/21

Example

While invoking this function if you pass the day and week values exceeding their range, they will be added to their parent values −

Live Demo
<?php
   //Creating a date
   $date = new DateTime();
   //Setting the date
   date_isodate_set($date, 2019, 15, 17);   
   print("Date: ".date_format($date, "Y/m/d"));
?>

This will produce the following output −

Date: 2019/04/24

Example

<?php
   $dateSrc = '2005-04-19 12:50 GMT';
   $dateTime = date_create( $dateSrc);;
   
   # Now set a new date using date_isodate_set();
   date_isodate_set( $dateTime, 2000, 12, 12);
   
   echo "New Formatted date is ". $dateTime->format("Y-m-d\TH:i:s\Z");
   echo "
"; # Using second function. $dateTime = new DateTime($dateSrc); $dateTime->setISODate( 1999, 10, 12); echo "New Formatted date is ". $dateTime->format("Y-m-d\TH:i:s\Z"); ?>

This will produce the following output −

New Formatted date is 2000-03-31T12:50:00Z
New Formatted date is 1999-03-19T12:50:00Z
php_function_reference.htm
Advertisements