English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Tutorial básico PHP

Tutorial avançado PHP

PHP & MySQL

Manual de referência PHP

Uso e exemplo da função PHP date_modify()

PHP Date & Time Function Manual

The date_modify() function modifies the value of the date-time (DateTime) object

Definition and usage

The date_modify() function is an alias of DateTime::modify(). This function is used to modify the date in the DateTime object. It changes the timestamp of the given object.

Syntax

date_modify($object, $modify)

Parameter

Serial numberParameters and descriptions
1

object (required)

This indicates the DateTime object you want to modify.

2

modify (required)

This is the date/time string specifying the required modification.

Return value

 Returns the modified DateTime object. If it fails, this function will return a boolean valuefalse.

PHP version

This function was initially introduced in PHP version5.2introduced in version 5.2.0 and can be used in all higher versions.

Online example

The following examples demonstratedate_modify()function usage-

<?php
   //Modify date
   $date = date_modify(new DateTime(), "+15 day");   
   print("Date: ".date_format($date, "Y/m/d");
?>
Test and see‹/›

Output result

Date: 2020/05/21

Online example

The following example creates a DateTime object and usesdate_modify()function to modify its date.-

<?php
   //Create a DateTime object
   $date_time_Obj = date_create("25-09-1989");
   print("Original date: ".date_format($date_time_Obj, "Y/m/d");
   print("\n");
   //Set date
   $date = date_modify($date_time_Obj, "+15 years 7 months 23 days");   
   print("Modify date: ".date_format($date, "Y/m/d");
?>
Test and see‹/›

Output result

Original date: 1989/09/25
Modify date: 2005/05/18

Online example

You can also modify the date by specifying the number of weeks, such as

<?php
   //Create DateTime object
   $date_time_Obj = date_create("25-09-1989");
   print("Original date: ".date_format($date_time_Obj, "Y/m/d");
   print("\n");
   //Set date
   $date = date_modify($date_time_Obj, "1960 weeks");   
   print("Modify date: ".date_format($date, "Y/m/d");
?>
Test and see‹/›

This will produce the following output-

Original date: 1989/09/25
Modify date: 2027/04/19

Online example

will add the specified date1day

<?php
   $date = new DateTime("1990-12-12");
   $date->modify("+1 day");
   
   echo $date->format("Y-m-d");
?>
Test and see‹/›

This will produce the following output-

1990-12-13