Open In App

PHP | date_date_set() Function

The date_date_set() function is an inbuilt function in PHP which is used to set a new Date. This function has four parameters $object, $year, $month and $day and returns DateTime object on success or false on failure. The date_date_set() function is an alias of DateTime::setDate() function.

Syntax:



date_date_set( $object, $year, $month, $day )

Parameters: This function accepts four parameters as mentioned above and described below:

Return Value:This function returns a new DateTime object on success or FALSE on failure.



Below programs illustrate the date_date_set() function in PHP.

Program 1: Procedural style to print DateTime.




<?php
  
// Date_create() returns a new DateTime object.
$date = date_create();
  
// date_date_set() function
date_date_set($date, 2018, 8, 31);
  
// Print the date in a format
echo date_format($date, "Y/m/d");
?>

Output:
2018/08/31

Program 2: Object oriented style to print DateTime.




<?php
  
// Declare DateTime object.
$date = new DateTime();
  
// date_date_set() function
$date->setDate(2018, 8, 31);
  
// Print the date in a format
echo $date->format('Y/m/d');
?>

Output:
2018/08/31

Related Articles:

Reference: http://php.net/manual/en/function.date-date-set.php


Article Tags :