English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
PHP Date & Time Functions Manual
A função date_date_set() define a data do objeto DateTime
A função date_date_set() é um alias para DateTime::setDate(). Usando esta função, você pode (re)definir a data do objeto DateTime.
date_date_set($object, $year, $month, $day)
Número de série | Parâmetros e descrição |
---|---|
1 | object(obrigatório) Este é um objeto DateTime, você precisa configurar a data para ele. |
2 | year(obrigatório) ano |
3 | month(obrigatório) mês |
4 | day(obrigatório) dia. |
Retorna o objeto DateTime modificado, se falhar, esta função retornará o valor booleano false.
This function was originally introduced in PHP version5.2.0 introduced in and can be used in all higher versions.
The following examples demonstratedate_date_setfunction usage-
<?php //Create date $date = new DateTime(); //Set date date_date_set($date, 2019, 07, 17); print("Date: " . date_format($date, "Y/m/d")); ?>Test to see‹/›
Output result
Date: 2019/07/17
The following example creates a DateTime object and usesdate_date_set()The function modifies its date.-
<?php //Date string $date_string = "25-09-1989"; //Create a DateTime object $date_time_Obj = date_create($date_string); print("Original Date: " . date_format($date_time_Obj, "Y/m/d")); print("\n"); //Set date $date = date_date_set($date_time_Obj, 2015, 11, 25 ); print("Modified Date: " . date_format($date, "Y/m/d")); ?>Test to see‹/›
Output result
Original Date: 1989/09/25 Modified Date: 2015/11/25
When calling this function, if the day and month values you pass exceed their range, they will be added to their parent value-
<?php //Create date $date = new DateTime(); //Set date date_date_set($date, 2019, 15, 17); print("Date: " . date_format($date, "Y/m/d")); ?>Test to see‹/›
Because we set the month value to15. Three months are added to the appropriate date-
Date: 2020/03/17
Use date_date_set() to set a new date
<?php $dateSrc = '2005-04-19 12:50 GMT'; $dateTime = date_create($dateSrc);; #Now use date_date_set() to set the new date; date_date_set($dateTime, 2000, 12, 12); echo "The new formatted date is " . $dateTime->format("Y-m-d\TH:i:s\Z"); echo "" "; #Use the second function. $dateTime = new DateTime($dateSrc); $dateTime->setDate( 1999, 10, 12); echo "The new formatted date is " . $dateTime->format("Y-m-d\TH:i:s\Z"); ?>Test to see‹/›
Output result:
The new formatted date is 2000-12-12T12:50:00Z The new formatted date is 1999-10-12T12:50:00Z