<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
  <channel>
    <title>Blog entries tagged pcre :: mwop.net</title>
    <description>Blog entries tagged pcre :: mwop.net</description>
    <pubDate>Thu, 16 Apr 2026 10:30:00 -0500</pubDate>
    <generator>Laminas_Feed_Writer 2 (https://getlaminas.org)</generator>
    <link>https://mwop.net/blog/tag/pcre</link>
    <atom:link rel="self" type="application/rss+xml" href="https://mwop.net/blog/tag/pcre/rss.xml"/>
    <item>
      <title>PHP DateTimeImmutable::createFromFormat Reset Character</title>
      <pubDate>Thu, 16 Apr 2026 10:30:00 -0500</pubDate>
      <link>https://mwop.net/blog/2026-04-16-php-create-from-format-reset.html</link>
      <guid>https://mwop.net/blog/2026-04-16-php-create-from-format-reset.html</guid>
      <author>contact@mwop.net (Matthew Weier O'Phinney)</author>
      <dc:creator>Matthew Weier O'Phinney</dc:creator>
      <content:encoded><![CDATA[<p>I was recently building something that was taking date input from an HTML form field, and casting it to a PHP <code>DateTimeImmutable</code>. I was then comparing that to another date, and got thrown off during testing when I compared the resulting instance to <code>new DateTimeImmutable('today')</code>; the instances were not considered equal.</p>


<p>To recreate the conditions, you can try the following:</p>
<pre><code class="language-php hljs php" data-lang="php">$date     = <span class="hljs-string">'2016-06-16'</span>;
$fromForm = DateTimeImmutable::createFromFormat(<span class="hljs-string">'Y-m-d'</span>, $date);
$today    = <span class="hljs-keyword">new</span> DateTimeImmutable(<span class="hljs-string">'today'</span>);
<span class="hljs-keyword">echo</span> $fromForm == $today ? <span class="hljs-string">'Equal'</span> : <span class="hljs-string">'Not equal'</span>; <span class="hljs-comment">// outputs "Not equal"</span>
</code></pre>
<p>What's happening? Well, if you were to echo the results of each of <code>$fromForm-&gt;format('c')</code> and <code>$today-&gt;format('c')</code>, the difference is clear: the <code>$fromForm</code> value includes the <em>time</em> when the instance was created, while <code>$today</code> has the time set to midnight.</p>
<p>So, how do you zero out the time when using <code>createFromFormat()</code>?</p>
<p>It turns out that one of the format characters you can use is the <code>|</code> operator. When you include this at the end of your format string, any fields not included in the format are zero'ed out:</p>
<pre><code class="language-php hljs php" data-lang="php">$fromForm = DateTimeImmutable::createFromFormat(<span class="hljs-string">'Y-m-d|'</span>);
</code></pre>
<hr />
<h4>Reference</h4>
<ul>
<li><a href="https://www.php.net/manual/en/datetimeimmutable.createfromformat.php#datetimeimmutable.createfromformat.parameters">DateTimeImmutable::createFromFormat() Parameters</a></li>
</ul>


<div class="h-entry">
    <img class="u-photo photo" width="50" src="https://avatars0.githubusercontent.com/u/25943?v=3&u=79dd2ea1d4d8855944715d09ee4c86215027fa80&s=140" alt="matthew">
    <a class="u-url u-uid p-name" href="https://mwop.net/blog/2026-04-16-php-create-from-format-reset.html">PHP DateTimeImmutable::createFromFormat Reset Character</a> was originally
    published <time class="dt-published" datetime="2026-04-16T10:30:00-05:00">16 April 2026</time>
    on <a href="https://mwop.net">https://mwop.net</a> by
    <a rel="author" class="p-author" href="https://mwop.net">Matthew Weier O&#039;Phinney</a>.
</div>
]]></content:encoded>
      <slash:comments>0</slash:comments>
    </item>
  </channel>
</rss>
