PHP - Function date_date_set()


Advertisements


Syntax

void date_date_set ( DateTime $object, int $year, int $month, int $day );

void DateTime::setDate ( int $year, int $month, int $day )

Definition and Usage

These functions set the date into given DateTime object. After this call object will have new date set.

The above two functions are equivalent and any of the functions can be used as shown below in the example.

Parameters

Sr.No Parameter & Description
1

object(Required)

DateTime object

2

year(Required)

Year of the date.

3

month(Required)

Month of the date.

4

day(Required)

Day of the date.

Return Value

It returns NULL on success or FALSE on failure.

Example

Following is the usage of this function −

Live Demo
<?php
   $dateSrc = '2005-04-19 12:50 GMT';
   $dateTime = date_create( $dateSrc);;
   # Now set a new date using date_date_set();
   date_date_set( $dateTime, 2000, 12, 12);
   
   echo "New Formatted date is ". $dateTime->format("Y-m-d\TH:i:s\Z");
   echo "<br />";

   # Using second function.
   $dateTime = new DateTime($dateSrc);
   $dateTime->setDate( 1999, 10, 12);
   
   echo "New Formatted date is ". $dateTime->format("Y-m-d\TH:i:s\Z");
?> 

This will produce the following result −

New Formatted date is 2000-12-12T12:50:00Z
New Formatted date is 1999-10-12T12:50:00Z

php_function_reference.htm

Advertisements