date_modify


php apg

ALTERS a particular TIMESTAMP.



<?php

/* - - - - - - - - - - - - - - - - -

    Object oriented style
   
   - - - - - - - - - - - - - - - - - */

DateTime|false public DateTime::modify str $modifier )


where,

$modifier The date/time STRING to be modified

?>

<?php

/* - - - - - - - - - - - - - - - - -

      Procedural style
   
   - - - - - - - - - - - - - - - - - */

DateTime|false date_modify DateTime $object str $modifier )


where,

$object A DateTime object returned by the function date_create

$modifier 
The date/time STRING to be modified

?>

 $object 


A DateTime object returned by the function date_create.



 $modifier 


The date/time STRING to be modified.



 TIME FORMATS 

Used Symbols
Description Formats Examples
frac . [0-9]+ ".21342", ".85"
hh "0"?[1-9] | "1"[0-2] "04", "7", "12"
HH [01][0-9] | "2"[0-4] "04", "07", "19"
meridian [AaPp] .? [Mm] .? [\0\t ] "A.m.", "pM", "am."
MM [0-5][0-9] "00", "12", "59"
II [0-5][0-9] "00", "12", "59"
space [ \t]  
tz "("? [A-Za-z]{1,6} ")"? | [A-Z][a-z]+([_/][A-Z][a-z]+)+ "CEST", "Europe/Amsterdam", "America/Indiana/Knox"
tzcorrection "GMT"? [+-] hh ":"? MM? "+0400", "GMT-07:00", "-07:00"
ed48

12 Hour Notation
Description Format Examples
Hour only, with meridian hh space? meridian "4 am", "5PM"
Hour and minutes, with meridian hh [.:] MM space? meridian "4:08 am", "7:19P.M."
Hour, minutes and seconds, with meridian hh [.:] MM [.:] II space? meridian "4:08:37 am", "7:19:19P.M."
MS SQL (Hour, minutes, seconds and fraction with meridian), PHP 5.3 and later only hh ":" MM ":" II [.:] [0-9]+ meridian "4:08:39:12313am"
ed48

24 Hour Notation
Description Format Examples
Hour and minutes 't'? HH [.:] MM "04:08", "19.19", "T23:43"
Hour and minutes, no colon 't'? HH MM "0408", "t1919", "T2343"
Hour, minutes and seconds 't'? HH [.:] MM [.:] II "04.08.37", "t19:19:19"
Hour, minutes and seconds, no colon 't'? HH MM II "040837", "T191919"
Hour, minutes, seconds and timezone 't'? HH [.:] MM [.:] II space? ( tzcorrection | tz ) "040837CEST", "T191919-0700"
Hour, minutes, seconds and fraction 't'? HH [.:] MM [.:] II frac "04.08.37.81412", "19:19:19.532453"
Time zone information tz | tzcorrection "CEST", "Europe/Amsterdam", "+0430", "GMT-06:00"
ed48


 DATE FORMATS 

Used Symbols
Description Format Examples
daysuf "st" | "nd" | "rd" | "th"  
dd ([0-2]?[0-9] | "3"[01]) daysuf? "7th", "22nd", "31"
DD "0" [0-9] | [1-2][0-9] | "3" [01] "07", "31"
m 'january' | 'february' | 'march' | 'april' | 'may' | 'june' | 'july' | 'august' | 'september' | 'october' | 'november' | 'december' | 'jan' | 'feb' | 'mar' | 'apr' | 'may' | 'jun' | 'jul' | 'aug' | 'sep' | 'sept' | 'oct' | 'nov' | 'dec' | "I" | "II" | "III" | "IV" | "V" | "VI" | "VII" | "VIII" | "IX" | "X" | "XI" | "XII"  
M 'jan' | 'feb' | 'mar' | 'apr' | 'may' | 'jun' | 'jul' | 'aug' | 'sep' | 'sept' | 'oct' | 'nov' | 'dec'  
mm "0"? [0-9] | "1"[0-2] "0", "04", "7", "12"
MM "0" [0-9] | "1"[0-2] "00", "04", "07", "12"
y [0-9]{1,4} "00", "78", "08", "8", "2008"
yy [0-9]{2} "00", "08", "78"
YY [0-9]{4} "2000", "2008", "1978"
ed48

Localized Notations
Description Format Examples
American month and day mm "/" dd "5/12", "10/27"
American month, day and year mm "/" dd "/" y "12/22/78", "1/17/2006", "1/17/6"
Four digit year, month and day with slashes YY "/" mm "/" dd "2008/6/30", "1978/12/22"
Four digit year and month (GNU) YY "-" mm "2008-6", "2008-06", "1978-12"
Year, month and day with dashes y "-" mm "-" dd "2008-6-30", "78-12-22", "8-6-21"
Day, month and four digit year, with dots, tabs or dashes dd [.\t-] mm [.-] YY "30-6-2008", "22.12.1978"
Day, month and two digit year, with dots or tabs dd [.\t] mm "." yy "30.6.08", "22\cr12.78"
Day, textual month and year dd ([ \t.-])* m ([ \t.-])* y "30-June 2008", "22DEC78", "14 III 1879"
Textual month and four digit year (Day reset to 1) m ([ \t.-])* YY "June 2008", "DEC1978", "March 1879"
Four digit year and textual month (Day reset to 1) YY ([ \t.-])* m "2008 June", "1978-XII", "1879.MArCH"
Textual month, day and year m ([ .\t-])* dd [,.stndrh\t ]+ y "July 1st, 2008", "April 17, 1790", "May.9,78"
Textual month and day m ([ .\t-])* dd [,.stndrh\t ]* "July 1st,", "Apr 17", "May.9"
Day and textual month d ([ .\t-])* m "1 July", "17 Apr", "9.May"
Month abbreviation, day and year M "-" DD "-" y "May-09-78", "Apr-17-1790"
Year, month abbreviation and day y "-" M "-" DD "78-Dec-22", "1814-MAY-17"
Year (and just the year) YY "1978", "2008"
Textual month (and just the month) m "March", "jun", "DEC"
ed48

ISO8601 Notations
Description Format Examples
Eight digit year, month and day YY MM DD "15810726", "19780417", "18140517"
Four digit year, month and day with slashes YY "/" MM "/" DD "2008/06/30", "1978/12/22"
Two digit year, month and day with dashes yy "-" MM "-" DD "08-06-30", "78-12-22"
Four digit year with optional sign, month and day [+-]? YY "-" MM "-" DD "-0002-07-26", "+1978-04-17", "1814-05-17"
ed48


 COMPOUND FORMATS 

Used Symbols
Description Formats Examples
DD "0" [0-9] | [1-2][0-9] | "3" [01] "02", "12", "31"
doy "00"[1-9] | "0"[1-9][0-9] | [1-2][0-9][0-9] | "3"[0-5][0-9] | "36"[0-6] "001", "012", "180", "350", "366"
frac . [0-9]+ ".21342", ".85"
hh "0"?[1-9] | "1"[0-2] "04", "7", "12"
HH [01][0-9] | "2"[0-4] "04", "07", "19"
meridian [AaPp] .? [Mm] .? [\0\t ] "A.m.", "pM", "am."
ii [0-5][0-9] "04", "8", "59"
II [0-5][0-9] "04", "08", "59"
M 'jan' | 'feb' | 'mar' | 'apr' | 'may' | 'jun' | 'jul' | 'aug' | 'sep' | 'sept' | 'oct' | 'nov' | 'dec'  
MM [0-1][0-9] "00", "12"
space [ \t]  
ss [0-5][0-9] "04", "8", "59"
SS [0-5][0-9] "04", "08", "59"
W "0"[1-9] | [1-4][0-9] | "5"[0-3] "05", "17", "53"
tzcorrection "GMT"? [+-] hh ":"? II? "+0400", "GMT-07:00", "-07:00"
YY [0-9]{4} "2000", "2008", "1978"
ed48

Localized Notations
Description Format Examples
Common Log Format dd "/" M "/" YY : HH ":" II ":" SS space tzcorrection "10/Oct/2000:13:55:36 -0700"
EXIF YY ":" MM ":" DD " " HH ":" II ":" SS "2008:08:07 18:11:31"
ISO year with ISO week YY "-"? "W" W "2008W27", "2008-W28"
ISO year with ISO week and day YY "-"? "W" W "-"? [0-7] "2008W273", "2008-W28-3"
MySQL YY "-" MM "-" DD " " HH ":" II ":" SS "2008-08-07 18:11:31"
PostgreSQL: Year with day-of-year YY "."? doy "2008.197", "2008197"
SOAP YY "-" MM "-" DD "T" HH ":" II ":" SS frac tzcorrection? "2008-07-01T22:35:17.02", "2008-07-01T22:35:17.03+08:00"
Unix Timestamp "@" "-"? [0-9]+ "@1215282385"
XMLRPC YY MM DD "T" hh ":" II ":" SS "20080701T22:38:07", "20080701T9:38:07"
XMLRPC (Compact) YY MM DD 't' hh II SS "20080701t223807", "20080701T093807"
WDDX YY "-" mm "-" dd "T" hh ":" ii ":" ss "2008-7-1T9:3:37"
ed48

The W in the ISO year with ISO week and ISO year with ISO week and day formats is case-sensitive, you can only use the upper case W.

The T in the SOAP, XMRPC and WDDX formats is case-sensitive, you can only use the upper case T.

The UNIX Timestamp format sets the timezone to UTC.



 RELATIVE FORMATS 

Used Symbols
Description Format
dayname 'sunday' | 'monday' | 'tuesday' | 'wednesday' | 'thursday' | 'friday' | 'saturday' | 'sun' | 'mon' | 'tue' | 'wed' | 'thu' | 'fri' | 'sat'
daytext 'weekday' | 'weekdays'
number [+-]?[0-9]+
ordinal 'first' | 'second' | 'third' | 'fourth' | 'fifth' | 'sixth' | 'seventh' | 'eighth' | 'ninth' | 'tenth' | 'eleventh' | 'twelfth' | 'next' | 'last' | 'previous' | 'this'
reltext 'next' | 'last' | 'previous' | 'this'
space [ \t]+
unit (('sec' | 'second' | 'min' | 'minute' | 'hour' | 'day' | 'fortnight' | 'forthnight' | 'month' | 'year') 's'?) | 'weeks' | daytext
ed48

Day-based Notations
Format Description Examples
'yesterday' Midnight of yesterday "yesterday 14:00"
'midnight' The time is set to 00:00:00  
'today' The time is set to 00:00:00  
'now' Now - this is simply ignored  
'noon' The time is set to 12:00:00 "yesterday noon"
'tomorrow' Midnight of tomorrow  
'back of' hour 15 minutes past the specified hour "back of 7pm", "back of 15"
'front of' hour 15 minutes before the specified hour "front of 5am", "front of 23"
'first day of' Sets the day of the first of the current month. This phrase is best used together with a month name following it. "first day of January 2008"
'last day of' Sets the day to the last day of the current month. This phrase is best used together with a month name following it. "last day of next month"
ordinal space dayname space 'of' Calculates the x-th week day of the current month. "first sat of July 2008"
'last' space dayname space 'of' Calculates the last week day of the current month. "last sat of July 2008"
number space? (unit | 'week') Handles relative time wtems where the value is a number. "+5 weeks", "12 day", "-7 weekdays"
ordinal space unit Handles relative time wtems where the value is text. "fifth day", "second month"
'ago' Negates all the values of previously found relative time wtems. "2 days ago", "8 days ago 14:00", "2 months 5 days ago", "2 months ago 5 days", "2 days ago"
dayname Moves to the next day of this name. "Monday"
reltext space 'week' Handles the special format "weekday + last/this/next week". "Monday next week"
ed48


This statments are always processed after all non-relative statments.

The highlighted format in the above table directly influence the current time.

dayname does not advance to another day.

number dayname does not advance to another day.

number week dayname will first add the number of weeks, but does not advance to another day.

ordinal dayname does advance to another day.

number week ordinal dayname will first add the number of weeks, and then advances to another day.

ordinal dayname 'of' does not advance to another day.

Also observe that the "of" in ordinal space dayname space 'of' and 'last' space dayname space 'of' does something special.

It sets the day-of-month to 1.

ordinal dayname 'of' does not advance to another day.

ordinal dayname does advance to another day.

'last' dayname 'of' takes the last dayname of the current month.

'last' dayname takes the last dayname from the current day.

Relative month values are calculated based on the length of months that they pass through.

An example would be +2 month 2011-11-30, which would produce 2012-01-30.

This is due to November being 30 days in length, and December being 31 days in length, producing a total of 61 days.

number is an integer number; if a decimal number is given, the dot, (or comma), is likely interpreted as delimiter.

For instance, '+1.5 hours' is parsed like '+1 5 hours', not as '+1 hour +30 minutes'.





This function must be used to alter the timestamp of a DateTime object by incrementing or decrementing in a format accepted by strtotime and returns the DateTime object for method chaining or FALSE on failure.



  1 EXERCISE   

<?php

$date01 
= new DateTime('2019-01-25');

$date01->modify('-2 month 2 week 10 day');

echo 
'format = ' $date01->format('Y-M-d') . '<br><br>';

echo 
'offset = ' $date01->getOffset() . '<br><br>';

echo 
'timestamp = ' $date01->getTimestamp() . '<br><br>';

?>

  2 EXERCISE   

<?php

$date02 
= new DateTime('now');

$date02->modify('tomorrow');

echo 
'format = ' $date02->format('Y-M-d') . '<br><br>';

echo 
'offset = ' $date02->getOffset() . '<br><br>';

echo 
'timestamp = ' $date02->getTimestamp() . '<br><br>';

?>

  3 EXERCISE   

<?php

$date03 
= new DateTime('1945-03-08 08:33:33');

$date03->modify('-0 hour');

echo 
$date03->format('Y-M-d D H:i:s') . '<br><br>';

?>

  4 EXERCISE   

<?php

date_default_timezone_set
("Pacific/Kwajalein");
$ts date_create("Fri Aug 20 1993 23:59:59");
echo 
date_format($ts'D, d M Y H:i:s T'), "<br><br>";
$ts->modify("+1 second");
echo 
date_format($ts'D, d M Y H:i:s T'), "<br><br>";

date_default_timezone_set("Europe/Amsterdam");
$ts date_create("Sun Mar 27 01:59:59 2005");
echo 
date_format($ts'D, d M Y H:i:s T'), "<br><br>";
$ts->modify("+1 second");
echo 
date_format($ts'D, d M Y H:i:s T'), "<br><br>";

$ts date_create("Sun Oct 30 01:59:59 2005");
echo 
date_format($ts'D, d M Y H:i:s T'), "<br><br>";
$ts->modify("+ 1 hour 1 second");
echo 
date_format($ts'D, d M Y H:i:s T'), "<br><br>";

?>

  5 EXERCISE   

<?php

date_default_timezone_set
("GMT");
$d date_create("2005-07-18 22:10:00 +0400");
echo 
date_format($d'D, d M Y H:i:s T'), "<br><br>";
date_modify($d"+1 hour");
echo 
date_format($d'D, d M Y H:i:s T'), "<br><br>";

?>

  6 EXERCISE   

<?php

/* - - - - - - - - - - - - - - - - -

      Procedural style
   
   - - - - - - - - - - - - - - - - - */

 //Set the default time zone
date_default_timezone_set("Asia/Tokyo");

echo 
"Testing date_modify() : basic functionality.<br><br><br>";

// Create a date object to modify
$datetime date_create("now");

date_modify($datetime"+0 day");
echo 
"After modification 1:<br>" date_format($datetime"D, d M Y H:i:s") . "<br><br>";

date_modify($datetime"+1 week 2 days 4 hours 2 seconds");
echo 
"After modification 2:<br>" date_format($datetime"D, d M Y H:i:s") . "<br><br>";

date_modify($datetime"next Thursday");
echo 
"After modification 3:<br>" date_format($datetime"D, d M Y") . "<br><br>";

date_modify($datetime"last Sunday");
echo 
"After modification 4:<br>" date_format($datetime"D, d M Y") . "<br><br>";

?>

  7 EXERCISE   

<?php
 
/* - - - - - - - - - - - - - - - - -

    Object oriented style
   
   - - - - - - - - - - - - - - - - - */
   
 //Set the default time zone
date_default_timezone_set("Asia/Tokyo");

echo 
"Testing date_modify() : basic functionality.<br><br><br>";

// Create a date object to modify
$datetime = new DateTime("now");

$datetime->modify("+0 day");
echo 
"After modification 1:<br>" $datetime->format("D, d M Y H:i:s") . "<br><br>";

$datetime->modify("+1 week 2 days 4 hours 2 seconds");
echo 
"After modification 2:<br>" $datetime->format("D, d M Y H:i:s") . "<br><br>";

$datetime->modify("next Thursday");
echo 
"After modification 3:<br>" $datetime->format("D, d M Y") . "<br><br>";

$datetime->modify("last Sunday");
echo 
"After modification 4:<br>" $datetime->format("D, d M Y") . "<br><br>";

?> 


  8 EXERCISE   

<?php

$date08 
= new DateTime('now');

$date08->modify('-2 month 2 week 10 day');

echo 
'format = ' $date08->format('Y-M-d') . '<br><br>';

echo 
'offset = ' $date08->getOffset() . '<br><br>';

echo 
'timestamp = ' $date08->getTimestamp() . '<br><br>';

echo 
'timezone = <pre>';
var_dump($date08->getTimezone());
echo 
'</pre>';

?>

  9 EXERCISE   

<?php

$date09 
= new DateTime();
date_modify($date09"Sunday next week");

foreach ([
   
'ATOM',
   
'COOKIE',
   
'ISO8601',
   
'RFC822',
   
'RFC850',
   
'RFC1036',
   
'RFC1123',
   
'RFC2822',
   
'RFC3339',
   
'RFC3339_EXTENDED',
   
'RSS',
   
'W3C',
   ] as 
$format) {
    eval(
"print 'DateTimeInterface::$format:<br>'.
    \$date09->format(DateTimeInterface::
$format).\"<br><br>\";");
    
// date_modify($date09, "+1 year");
}

?>

  10 EXERCISE   

<?php 

$date10 
= new DateTime('now');

echo 
'TODAY:<br>' $date10->format('D Y-M-d H:m:s');

echo 
'<br><br>';

echo 
'TIMESTAMP:<br>' $date10->getTimestamp();

echo 
'<br><br>';

$arr10 = ((array) $date10->getTimezone());

foreach (
$arr10 as $k10 => $v10)
{
echo 
strtoupper($k10) . ':<br>' $v10 '<br><br>';
}

date_modify($date10'next day');

echo 
'<br>24 h AHEAD:<br>' $date10->format('D Y-M-d H:m:s');

echo 
'<br><br>';

echo 
'TIMESTAMP:<br>' $date10->getTimestamp();

?>