Tuesday, November 17, 2020

PHP | date_isodate_set() Function

PHP date_isodate_set() Function


Hello dear readers! welcome back to another edition of our tutorial on PHP. In this tutorial guide, we are going to be discussing about the PHP date_isodate_set() Function.

PHP date_isodate_set() function is an alias of DateTime::setISODate(). Using this function, you can set an iso date. This makes use of weeks and days in a week.

Syntax

Following below is the syntax to use this function -

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


Parameter Details

Sr.NoParameter & 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 Value

This built-in PHP function returns the DateTime object with modified value. Incase of failure, it returns FALSE.

PHP Version

This function was first introduced as part of core PHP v 5.2.0 and, works with all the later versions.

Example1

Following example illustrates the usage of the date_isodate_set() function -

<?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"));
?>

Output

When the above code is executed, it will produce the following result -

Date: 2019/01/16

Example2

The 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"));
?>

Output

When the above code is executed, it will produce the following result -

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

Example3

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

<?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"));
?>

Output

When the above code is executed, it will produce the following result -

Date: 2019/04/24

Example4

Try the following 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"); ?>

Output

When the above code is executed, it will produce the following result -

New Formatted date is 2000-03-31T12:50:00Z
New Formatted date is 1999-03-19T12:50:00Z


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about the PHP date_modify() Function.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.
Share:

0 comments:

Post a Comment

Hello dear readers! Please kindly try your best to make sure your comments comply with our comment policy guidelines. You can visit our comment policy page to view these guidelines which are clearly stated. Thank you.