<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Alters the timestamp</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="datetime.getlasterrors.html">DateTime::getLastErrors</a></div> <div class="next" style="text-align: right; float: right;"><a href="datetime.set-state.html">DateTime::__set_state</a></div> <div class="up"><a href="class.datetime.html">DateTime</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="datetime.modify" class="refentry"> <div class="refnamediv"> <h1 class="refname">DateTime::modify</h1> <h1 class="refname">date_modify</h1> <p class="verinfo">(PHP 5 >= 5.2.0)</p><p class="refpurpose"><span class="refname">DateTime::modify</span> -- <span class="refname">date_modify</span> — <span class="dc-title">Alters the timestamp</span></p> </div> <div class="refsect1 description" id="refsect1-datetime.modify-description"> <h3 class="title">Description</h3> <p class="para">Object oriented style</p> <div class="methodsynopsis dc-description"> <span class="modifier">public</span> <span class="type"><a href="class.datetime.html" class="type DateTime">DateTime</a></span> <span class="methodname"><strong>DateTime::modify</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$modify</code></span> )</div> <p class="para rdfs-comment">Procedural style</p> <div class="methodsynopsis dc-description"> <span class="type"><a href="class.datetime.html" class="type DateTime">DateTime</a></span> <span class="methodname"><a href="function.date-modify.html" class="methodname">date_modify</a></span> ( <span class="methodparam"><span class="type"><a href="class.datetime.html" class="type DateTime">DateTime</a></span> <code class="parameter">$object</code></span> , <span class="methodparam"><span class="type">string</span> <code class="parameter">$modify</code></span> )</div> <p class="para rdfs-comment"> Alter the timestamp of a DateTime object by incrementing or decrementing in a format accepted by <span class="function"><a href="function.strtotime.html" class="function">strtotime()</a></span>. </p> </div> <div class="refsect1 parameters" id="refsect1-datetime.modify-parameters"> <h3 class="title">Parameters</h3> <dl> <dt> <span class="term"><em><code class="parameter">object</code></em></span> <dd> <p class="para">Procedural style only: A <a href="class.datetime.html" class="classname">DateTime</a> object returned by <span class="function"><a href="function.date-create.html" class="function">date_create()</a></span>. The function modifies this object.</p></dd> </dt> <dt> <span class="term"><em><code class="parameter">modify</code></em></span> <dd> <p class="para">A date/time string. Valid formats are explained in <a href="datetime.formats.html" class="link">Date and Time Formats</a>.</p> </dd> </dt> </dl> </div> <div class="refsect1 returnvalues" id="refsect1-datetime.modify-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the <a href="class.datetime.html" class="classname">DateTime</a> object for method chaining or <strong><code>FALSE</code></strong> on failure. </p> </div> <div class="refsect1 changelog" id="refsect1-datetime.modify-changelog"> <h3 class="title">Changelog</h3> <table class="doctable informaltable"> <thead> <tr> <th>Version</th> <th>Description</th> </tr> </thead> <tbody class="tbody"> <tr> <td>5.3.6</td> <td> Absolute date/time statements now take effect. Previously, only relative parts were used. </td> </tr> <tr><td>5.3.0</td><td>Changed the return value on success from <strong><code>NULL</code></strong> to <a href="class.datetime.html" class="classname">DateTime</a>.</td></tr> </tbody> </table> </div> <div class="refsect1 examples" id="refsect1-datetime.modify-examples"> <h3 class="title">Examples</h3> <div class="example" id="example-2258"> <p><strong>Example #1 <span class="function"><strong>DateTime::modify()</strong></span> example</strong></p> <div class="example-contents"><p>Object oriented style</p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$date </span><span style="color: #007700">= new </span><span style="color: #0000BB">DateTime</span><span style="color: #007700">(</span><span style="color: #DD0000">'2006-12-12'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$date</span><span style="color: #007700">-></span><span style="color: #0000BB">modify</span><span style="color: #007700">(</span><span style="color: #DD0000">'+1 day'</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$date</span><span style="color: #007700">-></span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'Y-m-d'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>Procedural style</p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$date </span><span style="color: #007700">= </span><span style="color: #0000BB">date_create</span><span style="color: #007700">(</span><span style="color: #DD0000">'2006-12-12'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">date_modify</span><span style="color: #007700">(</span><span style="color: #0000BB">$date</span><span style="color: #007700">, </span><span style="color: #DD0000">'+1 day'</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">date_format</span><span style="color: #007700">(</span><span style="color: #0000BB">$date</span><span style="color: #007700">, </span><span style="color: #DD0000">'Y-m-d'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above examples will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> 2006-12-13 </pre></div> </div> </div> <div class="example" id="example-2259"> <p><strong>Example #2 Beware when adding or subtracting months</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$date </span><span style="color: #007700">= new </span><span style="color: #0000BB">DateTime</span><span style="color: #007700">(</span><span style="color: #DD0000">'2000-12-31'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$date</span><span style="color: #007700">-></span><span style="color: #0000BB">modify</span><span style="color: #007700">(</span><span style="color: #DD0000">'+1 month'</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$date</span><span style="color: #007700">-></span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'Y-m-d'</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">$date</span><span style="color: #007700">-></span><span style="color: #0000BB">modify</span><span style="color: #007700">(</span><span style="color: #DD0000">'+1 month'</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$date</span><span style="color: #007700">-></span><span style="color: #0000BB">format</span><span style="color: #007700">(</span><span style="color: #DD0000">'Y-m-d'</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> 2001-01-31 2001-03-03 </pre></div> </div> </div> </div> <div class="refsect1 seealso" id="refsect1-datetime.modify-seealso"> <h3 class="title">See Also</h3> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.strtotime.html" class="function" rel="rdfs-seeAlso">strtotime()</a> - Parse about any English textual datetime description into a Unix timestamp</span></li> <li class="member"> <span class="function"><a href="datetime.add.html" class="function" rel="rdfs-seeAlso">DateTime::add()</a> - Adds an amount of days, months, years, hours, minutes and seconds to a DateTime object</span></li> <li class="member"> <span class="function"><a href="datetime.sub.html" class="function" rel="rdfs-seeAlso">DateTime::sub()</a> - Subtracts an amount of days, months, years, hours, minutes and seconds from a DateTime object</span></li> <li class="member"> <span class="function"><a href="datetime.setdate.html" class="function" rel="rdfs-seeAlso">DateTime::setDate()</a> - Sets the date</span></li> <li class="member"> <span class="function"><a href="datetime.setisodate.html" class="function" rel="rdfs-seeAlso">DateTime::setISODate()</a> - Sets the ISO date</span></li> <li class="member"> <span class="function"><a href="datetime.settime.html" class="function" rel="rdfs-seeAlso">DateTime::setTime()</a> - Sets the time</span></li> <li class="member"> <span class="function"><a href="datetime.settimestamp.html" class="function" rel="rdfs-seeAlso">DateTime::setTimestamp()</a> - Sets the date and time based on an Unix timestamp</span></li> </ul> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="datetime.getlasterrors.html">DateTime::getLastErrors</a></div> <div class="next" style="text-align: right; float: right;"><a href="datetime.set-state.html">DateTime::__set_state</a></div> <div class="up"><a href="class.datetime.html">DateTime</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>