<!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>Sets the date and time based on an Unix timestamp</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="datetime.settime.html">DateTime::setTime</a></div> <div class="next" style="text-align: right; float: right;"><a href="datetime.settimezone.html">DateTime::setTimezone</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.settimestamp" class="refentry"> <div class="refnamediv"> <h1 class="refname">DateTime::setTimestamp</h1> <h1 class="refname">date_timestamp_set</h1> <p class="verinfo">(PHP 5 >= 5.3.0)</p><p class="refpurpose"><span class="refname">DateTime::setTimestamp</span> -- <span class="refname">date_timestamp_set</span> — <span class="dc-title">Sets the date and time based on an Unix timestamp</span></p> </div> <div class="refsect1 description" id="refsect1-datetime.settimestamp-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::setTimestamp</strong></span> ( <span class="methodparam"><span class="type">int</span> <code class="parameter">$unixtimestamp</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-timestamp-set.html" class="methodname">date_timestamp_set</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">int</span> <code class="parameter">$unixtimestamp</code></span> )</div> <p class="para rdfs-comment"> Sets the date and time based on an Unix timestamp. </p> </div> <div class="refsect1 parameters" id="refsect1-datetime.settimestamp-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">unixtimestamp</code></em></span> <dd> <p class="para"> Unix timestamp representing the date. </p> </dd> </dt> </dl> </div> <div class="refsect1 returnvalues" id="refsect1-datetime.settimestamp-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 examples" id="refsect1-datetime.settimestamp-examples"> <h3 class="title">Examples</h3> <div class="example" id="example-2267"> <p><strong>Example #1 <span class="function"><strong>DateTime::setTimestamp()</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">();<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">'U = Y-m-d H:i:s'</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">setTimestamp</span><span style="color: #007700">(</span><span style="color: #0000BB">1171502725</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">'U = Y-m-d H:i:s'</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>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">();<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">'U = Y-m-d H:i:s'</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">date_timestamp_set</span><span style="color: #007700">(</span><span style="color: #0000BB">$date</span><span style="color: #007700">, </span><span style="color: #0000BB">1171502725</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">'U = Y-m-d H:i:s'</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 examples will output something similar to:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> 1272508903 = 2010-04-28 22:41:43 1171502725 = 2007-02-14 20:25:25 </pre></div> </div> </div> </div> <div class="refsect1 notes" id="refsect1-datetime.settimestamp-notes"> <h3 class="title">Notes</h3> <p class="para"> Using the Unix timestamp format to construct a new <span class="type"><a href="class.datetime.html" class="type DateTime">DateTime</a></span> object is an alternative when using PHP 5.2, as shown in the example below. </p> <div class="example" id="example-2268"> <p><strong>Example #2 <span class="function"><strong>DateTime::setTimestamp()</strong></span> alternative in PHP 5.2 </strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$ts </span><span style="color: #007700">= </span><span style="color: #0000BB">1171502725</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">$date </span><span style="color: #007700">= new </span><span style="color: #0000BB">DateTime</span><span style="color: #007700">(</span><span style="color: #DD0000">"@</span><span style="color: #0000BB">$ts</span><span style="color: #DD0000">"</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">'U = Y-m-d H:i:s'</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 something similar to:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> 1171502725 = 2007-02-14 20:25:25 </pre></div> </div> </div> </div> <div class="refsect1 seealso" id="refsect1-datetime.settimestamp-seealso"> <h3 class="title">See Also</h3> <ul class="simplelist"> <li class="member"> <span class="function"><a href="datetime.gettimestamp.html" class="function" rel="rdfs-seeAlso">DateTime::getTimestamp()</a> - Gets the 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.settime.html">DateTime::setTime</a></div> <div class="next" style="text-align: right; float: right;"><a href="datetime.settimezone.html">DateTime::setTimezone</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>