{"id":70,"date":"2019-09-06T04:30:53","date_gmt":"2019-09-06T04:30:53","guid":{"rendered":"https:\/\/tutorialio.com\/?p=70"},"modified":"2019-09-06T04:30:53","modified_gmt":"2019-09-06T04:30:53","slug":"php-datetime-class-methods","status":"publish","type":"post","link":"https:\/\/hellonitish.com\/blog\/php-datetime-class-methods\/","title":{"rendered":"PHP DateTime Class Methods"},"content":{"rendered":"<table class=\"quick-links\">\n<caption>DateTime class Methods in PHP<\/caption>\n<\/caption>\n<tbody>\n<tr>\n<th>Function Name<\/th>\n<th>Function Description<\/th>\n<\/tr>\n<tr>\n<td><a href=\"#datetimeadd\">DateTime::add()<\/a><\/td>\n<td>Adds an amount of days, months, years, hours, minutes and seconds to a DateTime object<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#datetimeconstruct\">DateTime::__construct()<\/a><\/td>\n<td>Returns new DateTime object<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#datetimecreatefromformat\">DateTime::createFromFormat()<\/a><\/td>\n<td>Parses a time string according to a specified format<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#datetimecreatefromimmutable\">DateTime::createFromImmutable()<\/a><\/td>\n<td>Returns new DateTime object encapsulating the given DateTimeImmutable object<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#datetimegetlasterrors\">DateTime::getLastErrors()<\/a><\/td>\n<td>Returns the warnings and errors<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#datetimemodify\">DateTime::modify()<\/a><\/td>\n<td>Alters the timestamp<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#datetimesetdate\">DateTime::setDate()<\/a><\/td>\n<td>Sets the date<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#datetimesetisodate\">DateTime::setISODate()<\/a><\/td>\n<td>Sets the ISO date<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#datetimesettime\">DateTime::setTime()<\/a><\/td>\n<td>Sets the time<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#datetimesettimestamp\">DateTime::setTimestamp()<\/a><\/td>\n<td>Sets the date and time based on an Unix timestamp<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#datetimesettimezone\">DateTime::setTimezone()<\/a><\/td>\n<td>Sets the time zone for the DateTime object<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#datetimesetstate\">DateTime::__set_state()<\/a><\/td>\n<td>The __set_state handler<\/td>\n<\/tr>\n<tr>\n<td><a href=\"#datetimesub\">DateTime::sub()<\/a><\/td>\n<td>Subtracts an amount of days, months, years, hours, minutes and seconds from a DateTime object<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h2 id=\"datetimeadd\">PHP <code>DateTime::add()<\/code> Method<\/h2>\n<h3>What does <code>DateTime::add()<\/code> do?<\/h3>\n<p>The PHP <code>DateTime::<code>add()<\/code><\/code> method adds an amount of days, months, years, hours, minutes and seconds to a DateTime object .<\/p>\n<h3>PHP <code>DateTime::add()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> public DateTime::add ( DateInterval $interval ) : DateTime<\/code><\/pre>\n<pre><code class=\"lang-php\"> date_add ( DateTime $object , DateInterval $interval ) : DateTime<\/code><\/pre>\n<h3>PHP <code>DateTime::add()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>object<\/code> \u2014 Procedural style only: A DateTime object returned by <code>date_create()<\/code>. The function modifies this object.<\/p>\n<\/li>\n<li>\n<p><code>interval<\/code> \u2014 A DateInterval object <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>DateTime::add()<\/code> Return Value<\/h3>\n<p>The PHP <code>DateTime::add()<\/code> method returns the DateTime object for method chaining or <code>FALSE<\/code> on failure.<\/p>\n<h3>PHP <code>DateTime::add()<\/code> Working Examples<\/h3>\n<h4>1. DateTime::add() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n$date = new DateTime('2000-01-01');\n$date-&gt;add(new DateInterval('P10D'));\necho $date-&gt;format('Y-m-d') . \"\\n\";\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">2000-01-11<\/code><\/pre>\n<h4>2. Further DateTime::add() examples<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n$date = new DateTime('2000-01-01');\n$date-&gt;add(new DateInterval('PT10H30S'));\necho $date-&gt;format('Y-m-d H:i:s') . \"\\n\";\n$date = new DateTime('2000-01-01');\n$date-&gt;add(new DateInterval('P7Y5M4DT4H3M2S'));\necho $date-&gt;format('Y-m-d H:i:s') . \"\\n\";\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">2000-01-01 10:00:30\n2007-06-05 04:03:02<\/code><\/pre>\n<h4>3. Beware when adding months<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n$date = new DateTime('2000-12-31');\n$interval = new DateInterval('P1M');\n$date-&gt;add($interval);\necho $date-&gt;format('Y-m-d') . \"\\n\";\n$date-&gt;add($interval);\necho $date-&gt;format('Y-m-d') . \"\\n\";\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">2001-01-31\n2001-03-03<\/code><\/pre>\n<h3>Additional Tips from Fellow Developers<\/h3>\n<p>Contributed By: Anonymous<\/p>\n<pre><code>Note that the add() and sub() methods will modify the value of the object you're calling the method on! This is very untypical for a method that returns a value of its own type. You could misunderstand it that the method would return a new instance with the modified value, but in fact it modifies itself! This is undocumented here. (Only a side note on procedural style mentions it, but it obviously does not apply to object oriented style.)<\/code><\/pre>\n<p>Contributed By: Angelo<\/p>\n<pre><code>Another simple solution to adding a month but not autocorrecting days to the next month is this.\n(Also works for substracting months)\n$dt = new DateTime(\"2016-01-31\");\n$oldDay = $dt-&gt;format(\"d\");\n$dt-&gt;add(new DateInterval(\"P1M\")); \/\/ 2016-03-02\n$newDay = $dt-&gt;format(\"d\");\nif($oldDay != $newDay) {\n    \/\/ Check if the day is changed, if so we skipped to the next month.\n    \/\/ Substract days to go back to the last day of previous month.\n    $dt-&gt;sub(new DateInterval(\"P\" . $newDay . \"D\"));\n}\necho $dt-&gt;format(\"Y-m-d\"); \/\/ 2016-02-29\nHope this helps someone.<\/code><\/pre>\n<p>Contributed By: Anthony<\/p>\n<pre><code>If you're using PHP &gt;= 5.5, instead of using \"glavic at gmail dot com\"'s DateTimeEnhanced class, use the built in DateTimeImmutable type. When you call DateTimeImmutable::add() it will return a new object, rather than modifying the original<\/code><\/pre>\n<p>Contributed By: patrick dot mckay7<\/p>\n<pre><code>Here is a solution to adding months when you want 2014-10-31 to become 2014-11-30 instead of 2014-12-01.\n&lt;?php\n\/**\n * Class MyDateTime\n *\n * Extends DateTime to include a sensible addMonth method.\n *\n * This class provides a method that will increment the month, and\n * if the day is greater than the last day in the new month, it\n * changes the day to the last day of that month. For example,\n * If you add one month to 2014-10-31 using DateTime::add, the\n * result is 2014-12-01. Using MyDateTime::addMonth the result is\n * 2014-11-30.\n *\/\nclass MyDateTime extends DateTime\n{\n    public function addMonth($num = 1)\n    {\n        $date = $this-&gt;format('Y-n-j');\n        list($y, $m, $d) = explode('-', $date);\n        $m += $num;\n        while ($m &gt; 12)\n        {\n            $m -= 12;\n            $y++;\n        }\n        $last_day = date('t', strtotime(\"$y-$m-1\"));\n        if ($d &gt; $last_day)\n        {\n            $d = $last_day;\n        }\n        $this-&gt;setDate($y, $m, $d);\n    }\n}\n?&gt;<\/code><\/pre>\n<p>Contributed By: glavic<\/p>\n<pre><code>If you need add() and sub() that don't modify object values, you can create new methods like this:\n&lt;?php\nclass DateTimeEnhanced extends DateTime {\n    public function returnAdd(DateInterval $interval)\n    {\n        $dt = clone $this;\n        $dt-&gt;add($interval);\n        return $dt;\n    }\n    \n    public function returnSub(DateInterval $interval)\n    {\n        $dt = clone $this;\n        $dt-&gt;sub($interval);\n        return $dt;\n    }\n}\n$interval = DateInterval::createfromdatestring('+1 day');\n$dt = new DateTimeEnhanced; # initialize new object\necho $dt-&gt;format(DateTime::W3C) . \"\\n\"; # 2013-09-12T15:01:44+02:00\n$dt-&gt;add($interval); # this modifies the object values\necho $dt-&gt;format(DateTime::W3C) . \"\\n\"; # 2013-09-13T15:01:44+02:00\n$dtNew = $dt-&gt;returnAdd($interval); # this returns the new modified object and doesn't change original object\necho $dt-&gt;format(DateTime::W3C) . \"\\n\"; # 2013-09-13T15:01:44+02:00\necho $dtNew-&gt;format(DateTime::W3C) . \"\\n\"; # 2013-09-14T15:01:44+02:00<\/code><\/pre>\n<h2 id=\"datetimeconstruct\">PHP <code>DateTime::__construct()<\/code> Method<\/h2>\n<h3>What does <code>DateTime::__construct()<\/code> do?<\/h3>\n<p>The PHP <code>DateTime::<code>__construct()<\/code><\/code> method will give you new DateTime object.<\/p>\n<h3>PHP <code>DateTime::__construct()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> date_create ([ string $datetime = \"now\" [, DateTimeZone $timezone = NULL ]] ) : DateTime<\/code><\/pre>\n<h3>PHP <code>DateTime::__construct()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>datetime<\/code> \u2014 A date\/time string. Valid formats are explained in Date and Time Formats.<\/p>\n<\/li>\n<li>\n<p><code>timezone<\/code> \u2014 A DateTimeZone object representing the timezone of $datetime. <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>DateTime::__construct()<\/code> Return Value<\/h3>\n<p>The PHP <code>DateTime::__construct()<\/code> method returns a new DateTime instance. Procedural style return <code>FALSE<\/code> on failure.<\/p>\n<h3>PHP <code>DateTime::__construct()<\/code> Working Examples<\/h3>\n<h4>1. DateTime::__construct() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\ntry {\n    $date = new DateTime('2000-01-01');\n} catch (Exception $e) {\n    echo $e-&gt;getMessage();\n    exit(1);\n}\necho $date-&gt;format('Y-m-d');\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">2000-01-01<\/code><\/pre>\n<h4>2. Intricacies of DateTime::__construct()<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n\/\/ Specified date\/time in your computer's time zone.\n$date = new DateTime('2000-01-01');\necho $date-&gt;format('Y-m-d H:i:sP') . \"\\n\";\n\/\/ Specified date\/time in the specified time zone.\n$date = new DateTime('2000-01-01', new DateTimeZone('Pacific\/Nauru'));\necho $date-&gt;format('Y-m-d H:i:sP') . \"\\n\";\n\/\/ Current date\/time in your computer's time zone.\n$date = new DateTime();\necho $date-&gt;format('Y-m-d H:i:sP') . \"\\n\";\n\/\/ Current date\/time in the specified time zone.\n$date = new DateTime(null, new DateTimeZone('Pacific\/Nauru'));\necho $date-&gt;format('Y-m-d H:i:sP') . \"\\n\";\n\/\/ Using a UNIX timestamp.  Notice the result is in the UTC time zone.\n$date = new DateTime('@946684800');\necho $date-&gt;format('Y-m-d H:i:sP') . \"\\n\";\n\/\/ Non-existent values roll over.\n$date = new DateTime('2000-02-30');\necho $date-&gt;format('Y-m-d H:i:sP') . \"\\n\";\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">2000-01-01 00:00:00-05:00\n2000-01-01 00:00:00+12:00\n2010-04-24 10:24:16-04:00\n2010-04-25 02:24:16+12:00\n2000-01-01 00:00:00+00:00\n2000-03-01 00:00:00-05:00<\/code><\/pre>\n<h3>Changelog for PHP DateTime::__construct()<\/code> Method<\/h3>\n<p><strong>7.1 \u2014 <\/strong>From now on microseconds are filled with actual value. Not with &#8216;00000&#8217;.<\/p>\n<p><strong>5.3.0 \u2014 <\/strong> If <code class=\"parameter\">datetime<\/code> contains an invalid date\/time format, then an exception is now thrown. Previously an error was emitted. <\/p>\n<h3>Important Points about PHP <code>DateTime::__construct()<\/code> Method<\/h3>\n<ol>\n<li>\n<p> The <code class=\"parameter\">$timezone<\/code> parameter and the current timezone are ignored when the <code class=\"parameter\">$datetime<\/code> parameter either is a UNIX timestamp (e.g. <strong>@946684800<\/strong>) or specifies a timezone (e.g. <strong>2010-01-28T15:00:00+02:00<\/strong>). <\/p>\n<\/li>\n<\/ol>\n<h3>Additional Tips from Fellow Developers<\/h3>\n<p>Contributed By: cHao<\/p>\n<pre><code>There's a reason for ignoring the time zone when you pass a timestamp to __construct.  That is, UNIX timestamps are by definition based on UTC.  @1234567890 represents the same date\/time regardless of time zone.  So there's no need for a time zone at all.<\/code><\/pre>\n<p>Contributed By: kendsnyder<\/p>\n<pre><code>The theoretical limits of the date range seem to be \"-9999-01-01\" through \"9999-12-31\" (PHP 5.2.9 on Windows Vista 64):\n&lt;?php\n$d = new DateTime(\"9999-12-31\"); \n$d-&gt;format(\"Y-m-d\"); \/\/ \"9999-12-31\"\n$d = new DateTime(\"0000-12-31\"); \n$d-&gt;format(\"Y-m-d\"); \/\/ \"0000-12-31\"\n$d = new DateTime(\"-9999-12-31\"); \n$d-&gt;format(\"Y-m-d\"); \/\/ \"-9999-12-31\"\n?&gt;\nDates above 10000 and below -10000 do not throw errors but produce weird results:\n&lt;?php\n$d = new DateTime(\"10019-01-01\"); \n$d-&gt;format(\"Y-m-d\"); \/\/ \"2009-01-01\"\n$d = new DateTime(\"10009-01-01\"); \n$d-&gt;format(\"Y-m-d\"); \/\/ \"2009-01-01\"\n$d = new DateTime(\"-10019-01-01\"); \n$d-&gt;format(\"Y-m-d\"); \/\/ \"2009-01-01\"\n?&gt;<\/code><\/pre>\n<p>Contributed By: joel dot kallman<\/p>\n<pre><code>A definite \"gotcha\" (while documented) that exists in the __construct is that it ignores your timezone if the $time is a timestamp.  While this may not make sense, the object does provide you with methods to work around it. \n \n&lt;?php \n\/\/ New Timezone Object \n$timezone = new DateTimeZone('America\/New_York'); \n \n\/\/ New DateTime Object \n$date =  new DateTime('@1306123200', $timezone);    \n \n\/\/ You would expect the date to be 2011-05-23 00:00:00 \n\/\/ But it actually outputs 2011-05-23 04:00:00 \necho $date-&gt;format('Y-m-d H:i:s'); \n \n\/\/ You can still set the timezone though like so...        \n$date-&gt;setTimezone($timezone); \n \n\/\/ This will now output 2011-05-23 00:00:00 \necho $date-&gt;format('Y-m-d H:i:s'); \n?&gt;<\/code><\/pre>\n<h2 id=\"datetimecreatefromformat\">PHP <code>DateTime::createFromFormat()<\/code> Method<\/h2>\n<h3>What does <code>DateTime::createFromFormat()<\/code> do?<\/h3>\n<p>The PHP <code>DateTime::<code>createFromFormat()<\/code><\/code> method parses a time string according to a specified format.<\/p>\n<h3>PHP <code>DateTime::createFromFormat()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> public static DateTime::createFromFormat ( string $format , string $datetime [, DateTimeZone $timezone ] ) : DateTime<\/code><\/pre>\n<pre><code class=\"lang-php\"> date_create_from_format ( string $format , string $datetime [, DateTimeZone $timezone ] ) : DateTime<\/code><\/pre>\n<h3>PHP <code>DateTime::createFromFormat()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>format<\/code> \u2014 The format that the passed in string should be in. See the formatting options below. In most cases, the same letters as for the <code>date()<\/code> can be used. <\/p>\n<\/li>\n<li>\n<p><code>datetime<\/code> \u2014 String representing the time. <\/p>\n<\/li>\n<li>\n<p><code>timezone<\/code> \u2014 A DateTimeZone object representing the desired time zone. <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>DateTime::createFromFormat()<\/code> Return Value<\/h3>\n<p>The PHP <code>DateTime::createFromFormat()<\/code> method returns a new DateTime instance or <code>FALSE<\/code> on failure.<\/p>\n<h3>PHP <code>DateTime::createFromFormat()<\/code> Working Examples<\/h3>\n<h4>1. DateTime::createFromFormat() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n$date = DateTime::createFromFormat('j-M-Y', '15-Feb-2009');\necho $date-&gt;format('Y-m-d');\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">2009-02-15<\/code><\/pre>\n<h4>2. Intricacies of DateTime::createFromFormat()<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\necho 'Current time: ' . date('Y-m-d H:i:s') . \"\\n\";\n$format = 'Y-m-d';\n$date = DateTime::createFromFormat($format, '2009-02-15');\necho \"Format: $format; \" . $date-&gt;format('Y-m-d H:i:s') . \"\\n\";\n$format = 'Y-m-d H:i:s';\n$date = DateTime::createFromFormat($format, '2009-02-15 15:16:17');\necho \"Format: $format; \" . $date-&gt;format('Y-m-d H:i:s') . \"\\n\";\n$format = 'Y-m-!d H:i:s';\n$date = DateTime::createFromFormat($format, '2009-02-15 15:16:17');\necho \"Format: $format; \" . $date-&gt;format('Y-m-d H:i:s') . \"\\n\";\n$format = '!d';\n$date = DateTime::createFromFormat($format, '15');\necho \"Format: $format; \" . $date-&gt;format('Y-m-d H:i:s') . \"\\n\";\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">Current time: 2010-04-23 10:29:35\nFormat: Y-m-d; 2009-02-15 10:29:35\nFormat: Y-m-d H:i:s; 2009-02-15 15:16:17\nFormat: Y-m-!d H:i:s; 1970-01-15 15:16:17\nFormat: !d; 1970-01-15 00:00:00<\/code><\/pre>\n<h4>3. Format string with literal characters<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\necho DateTime::createFromFormat('H\\h i\\m s\\s','23h 15m 03s')-&gt;format('H:i:s');\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">23:15:03<\/code><\/pre>\n<h3>Changelog for PHP DateTime::createFromFormat()<\/code> Method<\/h3>\n<p><strong>7.3.0 \u2014 <\/strong> The v <code class=\"parameter\">format<\/code> specifier has been added. <\/p>\n<p><strong>5.3.9 \u2014 <\/strong> The + <code class=\"parameter\">format<\/code> specifier has been added. <\/p>\n<h3>Important Points about PHP <code>DateTime::createFromFormat()<\/code> Method<\/h3>\n<ol>\n<li>\n<p> The <code class=\"parameter\">timezone<\/code> parameter and the current timezone are ignored when the <code class=\"parameter\">datetime<\/code> parameter either contains a UNIX timestamp (e.g. <strong>946684800<\/strong>) or specifies a timezone (e.g. <strong>2010-01-28T15:00:00+02:00<\/strong>). <\/p>\n<\/li>\n<\/ol>\n<h3>Additional Tips from Fellow Developers<\/h3>\n<p>Contributed By: falundir<\/p>\n<pre><code>Be warned that DateTime object created without explicitely providing the time portion will have the current time set instead of 00:00:00.\n&lt;?php\n$date = DateTime::createFromFormat('Y-m-d', '2012-10-17');\nvar_dump($date-&gt;format('Y-m-d H:i:s')); \/\/will print 2012-10-17 13:57:34 (the current time)\n?&gt;\nThat's also why you can't safely compare equality of such DateTime objects:\n&lt;?php\n$date1 = DateTime::createFromFormat('Y-m-d', '2012-10-17');\nsleep(2);\n$date2 = DateTime::createFromFormat('Y-m-d', '2012-10-17');\nvar_dump($date1 == $date2); \/\/will be false\nvar_dump($date1 &gt;= $date2); \/\/will be false\nvar_dump($date1 &lt; $date2); \/\/will be true\n?&gt;<\/code><\/pre>\n<p>Contributed By: Albie<\/p>\n<pre><code>If you want to safely compare equality of a DateTime object without explicitly providing the time portion make use of the ! format character.\n&lt;?php\n$date1 = DateTime::createFromFormat('!Y-m-d', '2012-10-17');\nsleep(2);\n$date2 = DateTime::createFromFormat('!Y-m-d', '2012-10-17');\n\/* \n $date1 and $date2 will both be set to a timestamp of \"2012-10-17 00:00:00\"\n var_dump($date1 == $date2); \/\/will be true\n var_dump($date1 &gt; $date2); \/\/will be false\n var_dump($date1 &lt; $date2); \/\/will be false\n*\/\n?&gt;\nIf you omit the ! format character without explicitly providing the time portion your timestamp which will include the current system time in the stamp.\n&lt;?php\n$date1 = DateTime::createFromFormat('Y-m-d', '2012-10-17');\nsleep(2);\n$date2 = DateTime::createFromFormat('Y-m-d', '2012-10-17');\nvar_dump($date1 == $date2); \/\/will be false\nvar_dump($date1 &gt;= $date2); \/\/will be false\nvar_dump($date1 &lt; $date2); \/\/will be true\n?&gt;<\/code><\/pre>\n<p>Contributed By: Fabian<\/p>\n<pre><code>Parsing RFC3339 strings can be very tricky when their are microseconds in the date string.\nSince PHP 7 there is the undocumented constant DateTime::RFC3339_EXTENDED (value: Y-m-d\\TH:i:s.vP), which can be used to output an RFC3339 string with microseconds:\n&lt;?php\n$d = new DateTime();\nvar_dump($d-&gt;format(DateTime::RFC3339_EXTENDED)); \/\/ 2017-07-25T13:47:12.000+00:00\n?&gt;\nBut the same constant can't be used for parsing an RFC3339 string with microseconds, instead do:\n&lt;?php\n$date = DateTime::createFromFormat(\"Y-m-d\\TH:i:s.uP\", \"2017-07-25T15:25:16.123456+02:00\")\n?&gt;\nBut \"u\" can only parse microseconds up to 6 digits, but some language (like Go) return more than 6 digits for the microseconds, e.g.: \"2017-07-25T15:50:42.456430712+02:00\" (when turning time.Time to JSON with json.Marshal()). Currently there is no other solution than using a separate parsing library to get correct dates.\nNote: the difference between \"v\" and \"u\" is just 3 digits vs. 6 digits.<\/code><\/pre>\n<p>Contributed By: thflori<\/p>\n<pre><code>CreateFromFormat('U') has a strange behaviour: it ignores the datetimezone and the resulting DateTime object will always have GMT+0000 timezone.\n&lt;?php\n$dt = DateTime::createFromFormat('U', time(), new DateTimeZone('CET'));\nvar_dump($dt-&gt;format('Y-m-d H:i:s (T)'), date('Y-m-d H:i:s (T)', time()));\n?&gt;\nThe problem is microtime() and time() returning the timestamp in current timezone.  Instead of using time you can use 'now' but to get a DateTimeObject with microseconds you have to write it this way to be sure to get the correct datetime:\n&lt;?php\n$dt = \\DateTime::createFromFormat('U.u', microtime(true))-&gt;setTimezone(new \\DateTimeZone(date('T')));\n?&gt;<\/code><\/pre>\n<p>Contributed By: d dot shankarnarayana<\/p>\n<pre><code>Say if there is a string with  $date = \"today is 2014 January 1\";   and you need to extract \"2014 January\" using DateTime::createFromFormat().  As you can see in the string there is something odd like \"today is\" .Since that string (today is) does not correspond to a date format, we need to escape that. \nIn this case, each and every character on that string has to be escaped as shown below.\nThe code.\n&lt;?php\n$paragraph = \"today is 2014 January 1\";\n$date = DateTime::createFromFormat('\\t\\o\\d\\a\\y \\i\\s Y F j', $paragraph);\necho $date-&gt;format('Y F'); \/\/\"prints\" 2014 January\n- Shankar Damodaran<\/code><\/pre>\n<p>Contributed By: lsloan-php dot net<\/p>\n<pre><code>Reportedly, microtime() may return a timestamp number without a fractional part if the microseconds are exactly zero.  I.e., \"1463772747\" instead of the expected \"1463772747.000000\".  number_format() can create a correct string representation of the microsecond timestamp every time, which can be useful for creating DateTime objects when used with DateTime::createFromFormat():\n&lt;?php\n$now = DateTime::createFromFormat('U.u', number_format(microtime(true), 6, '.', ''));\nvar_dump($now-&gt;format('Y-m-d H:i:s.u')); \/\/ E.g., string(26) \"2016-05-20 19:36:26.900794\"<\/code><\/pre>\n<p>Contributed By: tuxedobob<\/p>\n<pre><code>If you're here because you're trying to create a date from a week number, you want to be using setISODate, as I discovered here:\nhttp:\/\/www.lornajane.net\/posts\/2011\/getting-dates-from-week-numbers-in-php<\/code><\/pre>\n<p>Contributed By: ELPI<\/p>\n<pre><code>It can be confusing creating new DateTime from timestamp when your default timezone (date.timezone) is different from UTC and you are used to date()-function.\ndate()-function automatically uses your current timezone setting but DateTime::createFromFormat (or DateTime constructor) does not (it ignores tz-parameter).\nYou can get same results as date() by setting the timezone after object creation.\n&lt;?php\n$ts = 1414706400;\n$date1 = date(\"Y-m-d H:i\", $ts);\n$date2 = DateTime::createFromFormat(\"U\", $ts)-&gt;setTimeZone(new DateTimeZone(date_default_timezone_get()))-&gt;format(\"Y-m-d H:i\");\n\/\/$date1===$date2\n?&gt;<\/code><\/pre>\n<p>Contributed By: nicodoggie<\/p>\n<pre><code>I've found that on PHP 5.5.13 (not sure if it happens on other versions) if you enter a month larger than 12 on a format that takes numeric months, the result will be a DateTime object with its month equal to the number modulo 12 instead of returning false.\n&lt;?php\nvar_dump(DateTime::createFromFormat('Y-m-d', '2013-22-01'));\n?&gt;\nresults in:\nclass DateTime#4 (3) {\n  public $date =&gt;\n  string(19) \"2014-10-01 13:05:05\"\n  public $timezone_type =&gt;\n  int(3)\n  public $timezone =&gt;\n  string(3) \"UTC\"\n}<\/code><\/pre>\n<p>Contributed By: thomas dot ribiere<\/p>\n<pre><code>Not a bug, but a strange issue today 2012-08-30 :\n&lt;?php\n$date = \"2011-02\";\necho $date.\"\\n\";\n$d = DateTime::createFromFormat(\"Y-m\",$date);\necho $d-&gt;format(\"Y-m\");\n?&gt;\nwill display : \n2011-02\n2011-03\nIt's because there is no 2011-02-30, so datetime will take march insteed of february ...\nTo fix it :\n&lt;?php \n$date = \"2011-02\";\necho $date.\"\\n\";\n$d = DateTime::createFromFormat(\"Y-m-d\",$date.\"-01\");\necho $d-&gt;format(\"Y-m\");\n?&gt;<\/code><\/pre>\n<h2 id=\"datetimecreatefromimmutable\">PHP <code>DateTime::createFromImmutable()<\/code> Method<\/h2>\n<h3>What does <code>DateTime::createFromImmutable()<\/code> do?<\/h3>\n<p>The PHP <code>DateTime::<code>createFromImmutable()<\/code><\/code> method will give you new DateTime object encapsulating the given DateTimeImmutable object.<\/p>\n<h3>PHP <code>DateTime::createFromImmutable()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> public static DateTime::createFromImmutable ( DateTimeImmutable $object ) : DateTime<\/code><\/pre>\n<h3>PHP <code>DateTime::createFromImmutable()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>object<\/code> \u2014 The immutable DateTimeImmutable object that needs to be converted to a mutable version. This object is not modified, but instead a new DateTime object is created containing the same date, time, and timezone information. <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>DateTime::createFromImmutable()<\/code> Return Value<\/h3>\n<p>The PHP <code>DateTime::createFromImmutable()<\/code> method returns a new DateTime instance.<\/p>\n<h3>PHP <code>DateTime::createFromImmutable()<\/code> Working Examples<\/h3>\n<h4>1. Creating a mutable date time object<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n$date = new DateTimeImmutable(\"2014-06-20 11:45 Europe\/London\");\n$mutable = DateTime::createFromImmutable( $date );\n?&gt;<\/code><\/pre>\n<h2 id=\"datetimegetlasterrors\">PHP <code>DateTime::getLastErrors()<\/code> Method<\/h2>\n<h3>What does <code>DateTime::getLastErrors()<\/code> do?<\/h3>\n<p>The PHP <code>DateTime::<code>getLastErrors()<\/code><\/code> method will give you the warnings and errors.<\/p>\n<h3>PHP <code>DateTime::getLastErrors()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> public static DateTime::getLastErrors ( void ) : array<\/code><\/pre>\n<pre><code class=\"lang-php\"> date_get_last_errors ( void ) : array<\/code><\/pre>\n<h3>PHP <code>DateTime::getLastErrors()<\/code> Parameters<\/h3>\n<ol><\/ol>\n<p>This method does not accept any parameters.<\/p>\n<h3>PHP <code>DateTime::getLastErrors()<\/code> Return Value<\/h3>\n<p>The PHP <code>DateTime::getLastErrors()<\/code> method returns array containing info about warnings and errors.<\/p>\n<h3>PHP <code>DateTime::getLastErrors()<\/code> Working Examples<\/h3>\n<h4>1. DateTime::getLastErrors() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\ntry {\n    $date = new DateTime('asdfasdf');\n} catch (Exception $e) {\n    \/\/ For demonstration purposes only...\n    print_r(DateTime::getLastErrors());\n    \/\/ The real object oriented way to do this is\n    \/\/ echo $e-&gt;getMessage();\n}\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">Array\n(\n [warning_count] =&gt; 1\n [warnings] =&gt; Array\n (\n [6] =&gt; Double timezone specification\n )\n [error_count] =&gt; 1\n [errors] =&gt; Array\n (\n [0] =&gt; The timezone could not be found in the database\n )\n)<\/code><\/pre>\n<h2 id=\"datetimemodify\">PHP <code>DateTime::modify()<\/code> Method<\/h2>\n<h3>What does <code>DateTime::modify()<\/code> do?<\/h3>\n<p>The PHP <code>DateTime::<code>modify()<\/code><\/code> method alters the timestamp.<\/p>\n<h3>PHP <code>DateTime::modify()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> public DateTime::modify ( string $modifier ) : DateTime<\/code><\/pre>\n<pre><code class=\"lang-php\"> date_modify ( DateTime $object , string $modifier ) : DateTime<\/code><\/pre>\n<h3>PHP <code>DateTime::modify()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>object<\/code> \u2014 Procedural style only: A DateTime object returned by <code>date_create()<\/code>. The function modifies this object.<\/p>\n<\/li>\n<li>\n<p><code>modifier<\/code> \u2014 A date\/time string. Valid formats are explained in Date and Time Formats.<\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>DateTime::modify()<\/code> Return Value<\/h3>\n<p>The PHP <code>DateTime::modify()<\/code> method returns the DateTime object for method chaining or <code>FALSE<\/code> on failure.<\/p>\n<h3>PHP <code>DateTime::modify()<\/code> Working Examples<\/h3>\n<h4>1. DateTime::modify() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n$date = new DateTime('2006-12-12');\n$date-&gt;modify('+1 day');\necho $date-&gt;format('Y-m-d');\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">2006-12-13<\/code><\/pre>\n<h4>2. Beware when adding or subtracting months<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n$date = new DateTime('2000-12-31');\n$date-&gt;modify('+1 month');\necho $date-&gt;format('Y-m-d') . \"\\n\";\n$date-&gt;modify('+1 month');\necho $date-&gt;format('Y-m-d') . \"\\n\";\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">2001-01-31\n2001-03-03<\/code><\/pre>\n<h3>Changelog for PHP DateTime::modify()<\/code> Method<\/h3>\n<p><strong>5.3.6 \u2014 <\/strong> Absolute date\/time statements now take effect. Previously, only relative parts were used. <\/p>\n<p><strong>5.3.0 \u2014 <\/strong>Changed the return value on success from <code>NULL<\/code> to DateTime.<\/p>\n<h3>Additional Tips from Fellow Developers<\/h3>\n<p>Contributed By: mangotonk<\/p>\n<pre><code>A slightly more compact way of getting the month shift\n&lt;?php\n      \/**\n     * correctly calculates end of months when we shift to a shorter or longer month\n     * workaround for http:\/\/php.net\/manual\/en\/datetime.add.php#example-2489 \n     * \n     * Makes the assumption that shifting from the 28th Feb +1 month is 31st March\n     * Makes the assumption that shifting from the 28th Feb -1 month is 31st Jan\n     * Makes the assumption that shifting from the 29,30,31 Jan +1 month is 28th (or 29th) Feb\n     *\n     * \n     * @param DateTime $aDate\n     * @param int $months positive or negative\n     * \n     * @return DateTime new instance - original parameter is unchanged\n     *\/\n    function MonthShifter (DateTime $aDate,$months){\n        $dateA = clone($aDate);\n        $dateB = clone($aDate);\n        $plusMonths = clone($dateA-&gt;modify($months . ' Month'));\n        \/\/check whether reversing the month addition gives us the original day back\n        if($dateB != $dateA-&gt;modify($months*-1 . ' Month')){ \n            $result = $plusMonths-&gt;modify('last day of last month');\n        } elseif($aDate == $dateB-&gt;modify('last day of this month')){\n            $result =  $plusMonths-&gt;modify('last day of this month');\n        } else {\n            $result = $plusMonths;\n        }\n        return $result;\n    }\n\/\/TEST\n$x = new DateTime('2017-01-30');\necho( $x-&gt;format('Y-m-d').\" past end of feb, but not dec&lt;br&gt;\");\necho('b ' . MonthShifter($x,1)-&gt;format(('Y-m-d')).\"&lt;br&gt;\");\necho('c ' . MonthShifter($x,-1)-&gt;format(('Y-m-d')).\"&lt;br&gt;\");\n$x = new DateTime('2017-01-15');\necho(\"&lt;br&gt;\" . $x-&gt;format('Y-m-d').\" middle of the month &lt;br&gt;\");\necho('d ' . MonthShifter($x,1)-&gt;format(('Y-m-d')).\"&lt;br&gt;\");\necho('e ' . MonthShifter($x,-1)-&gt;format(('Y-m-d')).\"&lt;br&gt;\");\n$x = new DateTime('2017-02-28');\necho(\"&lt;br&gt;\" . $x-&gt;format('Y-m-d').\" end of Feb&lt;br&gt;\");\necho('f ' . MonthShifter($x,1)-&gt;format(('Y-m-d')).\"&lt;br&gt;\");\necho('g ' . MonthShifter($x,-1)-&gt;format(('Y-m-d')).\"&lt;br&gt;\");\n$x = new DateTime('2017-01-31');\necho(\"&lt;br&gt;\" .  $x-&gt;format('Y-m-d').\" end of Jan&lt;br&gt;\");\necho('h ' . MonthShifter($x,1)-&gt;format(('Y-m-d')).\"&lt;br&gt;\");\necho('i ' . MonthShifter($x,-1)-&gt;format(('Y-m-d')).\"&lt;br&gt;\");\n$x = new DateTime('2017-01-31');\necho(\"&lt;br&gt;\" .  $x-&gt;format('Y-m-d').\" end of Jan +\/- 1 years diff, leap year respected&lt;br&gt;\");\necho('j ' . MonthShifter($x,13)-&gt;format(('Y-m-d')).\"&lt;br&gt;\");\necho('k ' . MonthShifter($x,-11)-&gt;format(('Y-m-d')).\"&lt;br&gt;\");\n\/\/returns\n2017-01-30 past end of feb, but not dec\nb 2017-02-28\nc 2016-12-30\n2017-01-15 middle of the month \nd 2017-02-15\ne 2016-12-15\n2017-02-28end of Feb\nf 2017-03-31\ng 2017-01-31\n2017-01-31end of Jan\nh 2017-02-28\ni 2016-12-31\n2017-01-31end of Jan +\/- 1 years diff, leap year respected\nj 2018-02-28\nk 2016-02-29<\/code><\/pre>\n<p>Contributed By: jenspj<\/p>\n<pre><code>These functions makes sure that adding months or years always ends up in the month you would expect.  Works for positive and negative values\n&lt;?php\n      \n       \n    $date=new DateTime();\n    $date-&gt;setDate(2008,2,29);\n    \n    function addMonths($date,$months){\n         \n        $init=clone $date;\n        $modifier=$months.' months';\n        $back_modifier =-$months.' months';\n        \n        $date-&gt;modify($modifier);\n        $back_to_init= clone $date;\n        $back_to_init-&gt;modify($back_modifier);\n        \n        while($init-&gt;format('m')!=$back_to_init-&gt;format('m')){\n        $date-&gt;modify('-1 day')    ;\n        $back_to_init= clone $date;\n        $back_to_init-&gt;modify($back_modifier);    \n        }\n        \n        \/*\n        if($months&lt;0&amp;&amp;$date-&gt;format('m')&gt;$init-&gt;format('m'))\n        while($date-&gt;format('m')-12-$init-&gt;format('m')!=$months%12)\n        $date-&gt;modify('-1 day');\n        else\n        if($months&gt;0&amp;&amp;$date-&gt;format('m')&lt;$init-&gt;format('m'))\n        while($date-&gt;format('m')+12-$init-&gt;format('m')!=$months%12)\n        $date-&gt;modify('-1 day');\n        else\n        while($date-&gt;format('m')-$init-&gt;format('m')!=$months%12)\n        $date-&gt;modify('-1 day');\n        *\/\n        \n    }\n     \n    function addYears($date,$years){\n        \n        $init=clone $date;\n        $modifier=$years.' years';\n        $date-&gt;modify($modifier);\n        \n        while($date-&gt;format('m')!=$init-&gt;format('m'))\n        $date-&gt;modify('-1 day');\n        \n        \n    } \n    \n    \n    \n    addMonths($date,-1);\n     addYears($date,3);\n    \n    \n    echo $date-&gt;format('F j,Y');\n     \n \n?&gt;<\/code><\/pre>\n<h2 id=\"datetimesetdate\">PHP <code>DateTime::setDate()<\/code> Method<\/h2>\n<h3>What does <code>DateTime::setDate()<\/code> do?<\/h3>\n<p>The PHP <code>DateTime::<code>setDate()<\/code><\/code> method sets the date.<\/p>\n<h3>PHP <code>DateTime::setDate()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> public DateTime::setDate ( int $year , int $month , int $day ) : DateTime<\/code><\/pre>\n<pre><code class=\"lang-php\"> date_date_set ( DateTime $object , int $year , int $month , int $day ) : DateTime<\/code><\/pre>\n<h3>PHP <code>DateTime::setDate()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>object<\/code> \u2014 Procedural style only: A DateTime object returned by <code>date_create()<\/code>. The function modifies this object.<\/p>\n<\/li>\n<li>\n<p><code>year<\/code> \u2014 Year of the date. <\/p>\n<\/li>\n<li>\n<p><code>month<\/code> \u2014 Month of the date. <\/p>\n<\/li>\n<li>\n<p><code>day<\/code> \u2014 Day of the date. <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>DateTime::setDate()<\/code> Return Value<\/h3>\n<p>The PHP <code>DateTime::setDate()<\/code> method returns the DateTime object for method chaining or <code>FALSE<\/code> on failure.<\/p>\n<h3>PHP <code>DateTime::setDate()<\/code> Working Examples<\/h3>\n<h4>1. DateTime::setDate() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n$date = new DateTime();\n$date-&gt;setDate(2001, 2, 3);\necho $date-&gt;format('Y-m-d');\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">2001-02-03<\/code><\/pre>\n<h4>2. Values exceeding ranges are added to their parent values<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n$date = new DateTime();\n$date-&gt;setDate(2001, 2, 28);\necho $date-&gt;format('Y-m-d') . \"\\n\";\n$date-&gt;setDate(2001, 2, 29);\necho $date-&gt;format('Y-m-d') . \"\\n\";\n$date-&gt;setDate(2001, 14, 3);\necho $date-&gt;format('Y-m-d') . \"\\n\";\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">2001-02-28\n2001-03-01\n2002-02-03<\/code><\/pre>\n<h3>Changelog for PHP DateTime::setDate()<\/code> Method<\/h3>\n<p><strong>5.3.0 \u2014 <\/strong>Changed the return value on success from <code>NULL<\/code> to DateTime.<\/p>\n<h2 id=\"datetimesetisodate\">PHP <code>DateTime::setISODate()<\/code> Method<\/h2>\n<h3>What does <code>DateTime::setISODate()<\/code> do?<\/h3>\n<p>The PHP <code>DateTime::<code>setISODate()<\/code><\/code> method sets the ISO date.<\/p>\n<h3>PHP <code>DateTime::setISODate()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> public DateTime::setISODate ( int $year , int $week [, int $dayOfWeek = 1 ] ) : DateTime<\/code><\/pre>\n<pre><code class=\"lang-php\"> date_isodate_set ( DateTime $object , int $year , int $week [, int $dayOfWeek = 1 ] ) : DateTime<\/code><\/pre>\n<h3>PHP <code>DateTime::setISODate()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>object<\/code> \u2014 Procedural style only: A DateTime object returned by <code>date_create()<\/code>. The function modifies this object.<\/p>\n<\/li>\n<li>\n<p><code>year<\/code> \u2014 Year of the date. <\/p>\n<\/li>\n<li>\n<p><code>week<\/code> \u2014 Week of the date. <\/p>\n<\/li>\n<li>\n<p><code>dayOfWeek<\/code> \u2014 Offset from the first day of the week. <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>DateTime::setISODate()<\/code> Return Value<\/h3>\n<p>The PHP <code>DateTime::setISODate()<\/code> method returns the DateTime object for method chaining or <code>FALSE<\/code> on failure.<\/p>\n<h3>PHP <code>DateTime::setISODate()<\/code> Working Examples<\/h3>\n<h4>1. DateTime::setISODate() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n$date = new DateTime();\n$date-&gt;setISODate(2008, 2);\necho $date-&gt;format('Y-m-d') . \"\\n\";\n$date-&gt;setISODate(2008, 2, 7);\necho $date-&gt;format('Y-m-d') . \"\\n\";\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">2008-01-07\n2008-01-13<\/code><\/pre>\n<h4>2. Values exceeding ranges are added to their parent values<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n$date = new DateTime();\n$date-&gt;setISODate(2008, 2, 7);\necho $date-&gt;format('Y-m-d') . \"\\n\";\n$date-&gt;setISODate(2008, 2, 8);\necho $date-&gt;format('Y-m-d') . \"\\n\";\n$date-&gt;setISODate(2008, 53, 7);\necho $date-&gt;format('Y-m-d') . \"\\n\";\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">2008-01-13\n2008-01-14\n2009-01-04<\/code><\/pre>\n<h4>3. Finding the month a week is in<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n$date = new DateTime();\n$date-&gt;setISODate(2008, 14);\necho $date-&gt;format('n');\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">3<\/code><\/pre>\n<h3>Changelog for PHP DateTime::setISODate()<\/code> Method<\/h3>\n<p><strong>5.3.0 \u2014 <\/strong>Changed the return value on success from <code>NULL<\/code> to DateTime.<\/p>\n<h2 id=\"datetimesettime\">PHP <code>DateTime::setTime()<\/code> Method<\/h2>\n<h3>What does <code>DateTime::setTime()<\/code> do?<\/h3>\n<p>The PHP <code>DateTime::<code>setTime()<\/code><\/code> method sets the time.<\/p>\n<h3>PHP <code>DateTime::setTime()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> public DateTime::setTime ( int $hour , int $minute [, int $second = 0 [, int $microsecond = 0 ]] ) : DateTime<\/code><\/pre>\n<pre><code class=\"lang-php\"> date_time_set ( DateTime $object , int $hour , int $minute [, int $second = 0 [, int $microsecond = 0 ]] ) : DateTime<\/code><\/pre>\n<h3>PHP <code>DateTime::setTime()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>object<\/code> \u2014 Procedural style only: A DateTime object returned by <code>date_create()<\/code>. The function modifies this object.<\/p>\n<\/li>\n<li>\n<p><code>hour<\/code> \u2014 Hour of the time. <\/p>\n<\/li>\n<li>\n<p><code>minute<\/code> \u2014 Minute of the time. <\/p>\n<\/li>\n<li>\n<p><code>second<\/code> \u2014 Second of the time. <\/p>\n<\/li>\n<li>\n<p><code>microsecond<\/code> \u2014 Microsecond of the time. <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>DateTime::setTime()<\/code> Return Value<\/h3>\n<p>The PHP <code>DateTime::setTime()<\/code> method returns the DateTime object for method chaining or <code>FALSE<\/code> on failure.<\/p>\n<h3>PHP <code>DateTime::setTime()<\/code> Working Examples<\/h3>\n<h4>1. DateTime::setTime() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n$date = new DateTime('2001-01-01');\n$date-&gt;setTime(14, 55);\necho $date-&gt;format('Y-m-d H:i:s') . \"\\n\";\n$date-&gt;setTime(14, 55, 24);\necho $date-&gt;format('Y-m-d H:i:s') . \"\\n\";\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">2001-01-01 14:55:00\n2001-01-01 14:55:24<\/code><\/pre>\n<h4>2. Values exceeding ranges are added to their parent values<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n$date = new DateTime('2001-01-01');\n$date-&gt;setTime(14, 55, 24);\necho $date-&gt;format('Y-m-d H:i:s') . \"\\n\";\n$date-&gt;setTime(14, 55, 65);\necho $date-&gt;format('Y-m-d H:i:s') . \"\\n\";\n$date-&gt;setTime(14, 65, 24);\necho $date-&gt;format('Y-m-d H:i:s') . \"\\n\";\n$date-&gt;setTime(25, 55, 24);\necho $date-&gt;format('Y-m-d H:i:s') . \"\\n\";\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">2001-01-01 14:55:24\n2001-01-01 14:56:05\n2001-01-01 15:05:24\n2001-01-02 01:55:24<\/code><\/pre>\n<h3>Changelog for PHP DateTime::setTime()<\/code> Method<\/h3>\n<p><strong>7.1.0 \u2014 <\/strong>The <code class=\"parameter\">microsecond<\/code> parameter was added.<\/p>\n<p><strong>5.3.0 \u2014 <\/strong>Changed the return value on success from <code>NULL<\/code> to DateTime.<\/p>\n<h2 id=\"datetimesettimestamp\">PHP <code>DateTime::setTimestamp()<\/code> Method<\/h2>\n<h3>What does <code>DateTime::setTimestamp()<\/code> do?<\/h3>\n<p>The PHP <code>DateTime::<code>setTimestamp()<\/code><\/code> method sets the date and time based on an Unix timestamp.<\/p>\n<h3>PHP <code>DateTime::setTimestamp()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> public DateTime::setTimestamp ( int $timestamp ) : DateTime<\/code><\/pre>\n<pre><code class=\"lang-php\"> date_timestamp_set ( DateTime $object , int $timestamp ) : DateTime<\/code><\/pre>\n<h3>PHP <code>DateTime::setTimestamp()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>object<\/code> \u2014 Procedural style only: A DateTime object returned by <code>date_create()<\/code>. The function modifies this object.<\/p>\n<\/li>\n<li>\n<p><code>timestamp<\/code> \u2014 Unix timestamp representing the date. <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>DateTime::setTimestamp()<\/code> Return Value<\/h3>\n<p>The PHP <code>DateTime::setTimestamp()<\/code> method returns the DateTime object for method chaining or <code>FALSE<\/code> on failure.<\/p>\n<h3>PHP <code>DateTime::setTimestamp()<\/code> Working Examples<\/h3>\n<h4>1. DateTime::setTimestamp() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n$date = new DateTime();\necho $date-&gt;format('U = Y-m-d H:i:s') . \"\\n\";\n$date-&gt;setTimestamp(1171502725);\necho $date-&gt;format('U = Y-m-d H:i:s') . \"\\n\";\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">1272508903 = 2010-04-28 22:41:43\n1171502725 = 2007-02-14 20:25:25<\/code><\/pre>\n<h4>2. DateTime::setTimestamp() alternative in PHP 5.2 <\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n$ts = 1171502725;\n$date = new DateTime(\"@$ts\");\necho $date-&gt;format('U = Y-m-d H:i:s') . \"\\n\";\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">1171502725 = 2007-02-14 20:25:25<\/code><\/pre>\n<h3>Additional Tips from Fellow Developers<\/h3>\n<p>Contributed By: admin<\/p>\n<pre><code>It should be noted above, be careful when manipulating the DateTime object with unix timestamps.\nIn the above examples you will get varying results dependent on your current timezone, method used, and version of PHP.\nOne would expect all of the examples above to perform the same as setTimestamp() or date('H:i', $timestamp); would.\n&lt;?php\ndate_default_timezone_set('America\/New_York');\n$ts = 1171502725;\n?&gt;\nSet timestamp from UTC timezone use UTC timezone\n&lt;?php\n$date = new DateTime(\"@$ts\"); \nvar_dump($date-&gt;format('Y-m-d H:i:s e'));\n\/*\nstring(26) \"2007-02-15 01:25:25 +00:00\" \/\/PHP 5.3.0 - 5.6.8\n*\/\n?&gt;\nTo convert the above to use the current timezone simply use\n&lt;?php\n$date-&gt;setTimezone(date_default_timezone_get());\n\/\/string(36) \"2007-02-14 20:25:25 America\/New_York\"\n?&gt;\nSet the timestamp from UTC timezone use current timezone\n&lt;?php\n$date = new DateTime;\n$date-&gt;modify('@' . $ts); \nvar_dump($date-&gt;format('Y-m-d H:i:s e'));\n\/*\nstring(36) \"2007-02-15 01:25:25 America\/New_York\" \/\/PHP 5.3.6 - 5.6.8\nstring(36) \"2052-06-20 18:53:24 America\/New_York\" \/\/PHP 5.3.0 - 5.3.5\n*\/\n?&gt;\nSet the timestamp from current timezone use current timezone\n&lt;?php\n$date = new DateTime;\n$date-&gt;setTimestamp($ts); \nvar_dump($date-&gt;format('Y-m-d H:i:s e'));\n\/*\nstring(36) \"2007-02-14 20:25:25 America\/New_York\" \/\/PHP 5.3.0 - 5.6.8\n*\/\n?&gt;<\/code><\/pre>\n<h2 id=\"datetimesettimezone\">PHP <code>DateTime::setTimezone()<\/code> Method<\/h2>\n<h3>What does <code>DateTime::setTimezone()<\/code> do?<\/h3>\n<p>The PHP <code>DateTime::<code>setTimezone()<\/code><\/code> method sets the time zone for the DateTime object.<\/p>\n<h3>PHP <code>DateTime::setTimezone()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> public DateTime::setTimezone ( DateTimeZone $timezone ) : DateTime<\/code><\/pre>\n<pre><code class=\"lang-php\"> date_timezone_set ( DateTime $object , DateTimeZone $timezone ) : DateTime<\/code><\/pre>\n<h3>PHP <code>DateTime::setTimezone()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>object<\/code> \u2014 Procedural style only: A DateTime object returned by <code>date_create()<\/code>. The function modifies this object.<\/p>\n<\/li>\n<li>\n<p><code>timezone<\/code> \u2014 A DateTimeZone object representing the desired time zone. <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>DateTime::setTimezone()<\/code> Return Value<\/h3>\n<p>The PHP <code>DateTime::setTimezone()<\/code> method returns the DateTime object for method chaining or <code>FALSE<\/code> on failure.<\/p>\n<h3>PHP <code>DateTime::setTimezone()<\/code> Working Examples<\/h3>\n<h4>1. DateTime::setTimeZone() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n$date = new DateTime('2000-01-01', new DateTimeZone('Pacific\/Nauru'));\necho $date-&gt;format('Y-m-d H:i:sP') . \"\\n\";\n$date-&gt;setTimezone(new DateTimeZone('Pacific\/Chatham'));\necho $date-&gt;format('Y-m-d H:i:sP') . \"\\n\";\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">2000-01-01 00:00:00+12:00\n2000-01-01 01:45:00+13:45<\/code><\/pre>\n<h3>Changelog for PHP DateTime::setTimezone()<\/code> Method<\/h3>\n<p><strong>5.3.0 \u2014 <\/strong>Changed the return value on success from <code>NULL<\/code> to DateTime.<\/p>\n<h3>Additional Tips from Fellow Developers<\/h3>\n<p>Contributed By: jamsilver<\/p>\n<pre><code>In response to the other comments expressing surprise that changing the timezone does not affect the timestamp:\nA UNIX timestamp is defined as the number of seconds that have elapsed since 00:00:00 (UTC), Thursday, 1 January 1970.\nSo: with respect to UTC. Always.\nCalling setTimezone() never changes the actual \"absolute\", underlying, moment-in-time itself. It only changes the timezone you wish to \"view\" that moment \"from\". Consider the following:\n&lt;?php\n\/\/ A time in London.\n$datetime = new DateTime('2015-06-22T10:40:25', new DateTimeZone('Europe\/London'));\n\/\/ I wonder how that SAME moment-in-time would \n\/\/ be described in other places around the world.\n$datetime-&gt;setTimezone(new DateTimeZone('Australia\/Sydney'));\nprint $datetime-&gt;format('Y-m-d H:i:s (e)');\n  \/\/ 2015-06-22 19:40:25 (Australia\/Sydney)\n$datetime-&gt;setTimezone(new DateTimeZone('America\/New_York'));\nprint $datetime-&gt;format('Y-m-d H:i:s (e)');\n  \/\/ 2015-06-22 05:40:25 (America\/New_York)\n$datetime-&gt;setTimezone(new DateTimeZone('Asia\/Calcutta'));\nprint $datetime-&gt;format('Y-m-d H:i:s (e)');\n  \/\/ 2015-06-22 15:10:25 (Asia\/Calcutta)\n?&gt;\nPlease note that ALL of these date strings unambiguously represent the exact same moment-in-time. Therefore, calling getTimestamp() at any stage will return the same result:\n&lt;?php\n$datetime-&gt;getTimestamp();\n  \/\/ 1434966025\n?&gt;<\/code><\/pre>\n<h2 id=\"datetimesetstate\">PHP <code>DateTime::__set_state()<\/code> Method<\/h2>\n<h3>What does <code>DateTime::__set_state()<\/code> do?<\/h3>\n<p>The PHP <code>DateTime::<code>__set_state()<\/code><\/code> method the __set_state handler.<\/p>\n<h3>PHP <code>DateTime::__set_state()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> public static DateTime::__set_state ( array $array ) : DateTime<\/code><\/pre>\n<h3>PHP <code>DateTime::__set_state()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>array<\/code> \u2014 Initialization array. <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>DateTime::__set_state()<\/code> Return Value<\/h3>\n<p>The PHP <code>DateTime::__set_state()<\/code> method returns a new instance of a DateTime object.<\/p>\n<h2 id=\"datetimesub\">PHP <code>DateTime::sub()<\/code> Method<\/h2>\n<h3>What does <code>DateTime::sub()<\/code> do?<\/h3>\n<p>The PHP <code>DateTime::<code>sub()<\/code><\/code> method subtracts an amount of days, months, years, hours, minutes and seconds from a DateTime object .<\/p>\n<h3>PHP <code>DateTime::sub()<\/code> Syntax<\/h3>\n<pre><code class=\"lang-php\"> public DateTime::sub ( DateInterval $interval ) : DateTime<\/code><\/pre>\n<pre><code class=\"lang-php\"> date_sub ( DateTime $object , DateInterval $interval ) : DateTime<\/code><\/pre>\n<h3>PHP <code>DateTime::sub()<\/code> Parameters<\/h3>\n<ol>\n<li>\n<p><code>object<\/code> \u2014 Procedural style only: A DateTime object returned by <code>date_create()<\/code>. The function modifies this object.<\/p>\n<\/li>\n<li>\n<p><code>interval<\/code> \u2014 A DateInterval object <\/p>\n<\/li>\n<\/ol>\n<h3>PHP <code>DateTime::sub()<\/code> Return Value<\/h3>\n<p>The PHP <code>DateTime::sub()<\/code> method returns the DateTime object for method chaining or <code>FALSE<\/code> on failure.<\/p>\n<h3>PHP <code>DateTime::sub()<\/code> Working Examples<\/h3>\n<h4>1. DateTime::sub() example<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n$date = new DateTime('2000-01-20');\n$date-&gt;sub(new DateInterval('P10D'));\necho $date-&gt;format('Y-m-d') . \"\\n\";\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">2000-01-10<\/code><\/pre>\n<h4>2. Further DateTime::sub() examples<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n$date = new DateTime('2000-01-20');\n$date-&gt;sub(new DateInterval('PT10H30S'));\necho $date-&gt;format('Y-m-d H:i:s') . \"\\n\";\n$date = new DateTime('2000-01-20');\n$date-&gt;sub(new DateInterval('P7Y5M4DT4H3M2S'));\necho $date-&gt;format('Y-m-d H:i:s') . \"\\n\";\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">2000-01-19 13:59:30\n1992-08-15 19:56:58<\/code><\/pre>\n<h4>3. Beware when subtracting months<\/h4>\n<pre><code class=\"lang-php\">&lt;?php\n$date = new DateTime('2001-04-30');\n$interval = new DateInterval('P1M');\n$date-&gt;sub($interval);\necho $date-&gt;format('Y-m-d') . \"\\n\";\n$date-&gt;sub($interval);\necho $date-&gt;format('Y-m-d') . \"\\n\";\n?&gt;<\/code><\/pre>\n<p>Output of the above code:<\/p>\n<pre><code class=\"lang-php\">2001-03-30\n2001-03-02<\/code><\/pre>\n<h3>Additional Tips from Fellow Developers<\/h3>\n<p>Contributed By: Anonymous<\/p>\n<pre><code>Note that the sub() and add() methods will modify the value of the object you're calling the method on! This is very untypical for a method that returns a value of its own type. You could misunderstand it that the method would return a new instance with the modified value, but in fact it modifies itself! This is undocumented here. (Only a side note on procedural style mentions it, but it obviously does not apply to object oriented style.)<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>DateTime class Methods in PHP Function Name Function Description DateTime::add() Adds an amount of days, months, years, hours, minutes and seconds to a DateTime object DateTime::__construct() Returns new DateTime object DateTime::createFromFormat() Parses a time string according to a specified format DateTime::createFromImmutable() Returns new DateTime object encapsulating the given DateTimeImmutable object DateTime::getLastErrors() Returns the warnings and [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[7],"tags":[9,5],"class_list":["post-70","post","type-post","status-publish","format-standard","hentry","category-reference","tag-date-time","tag-php"],"_links":{"self":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/70","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/comments?post=70"}],"version-history":[{"count":0,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/posts\/70\/revisions"}],"wp:attachment":[{"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/media?parent=70"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/categories?post=70"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/hellonitish.com\/blog\/wp-json\/wp\/v2\/tags?post=70"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}