<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title type="text">Blog entries tagged pcre :: mwop.net</title>
  <updated>2026-04-16T10:30:00-05:00</updated>
  <generator uri="https://getlaminas.org" version="2">Laminas_Feed_Writer</generator>
  <link rel="alternate" type="text/html" href="https://mwop.net/blog/tag/pcre"/>
  <link rel="self" type="application/atom+xml" href="https://mwop.net/blog/tag/pcre/atom.xml"/>
  <id>https://mwop.net/blog/tag/pcre</id>
  <entry xmlns:xhtml="http://www.w3.org/1999/xhtml">
    <title type="html"><![CDATA[PHP DateTimeImmutable::createFromFormat Reset Character]]></title>
    <published>2026-04-16T10:30:00-05:00</published>
    <updated>2026-04-16T10:30:00-05:00</updated>
    <link rel="alternate" type="text/html" href="https://mwop.net/blog/2026-04-16-php-create-from-format-reset.html"/>
    <id>https://mwop.net/blog/2026-04-16-php-create-from-format-reset.html</id>
    <author>
      <name>Matthew Weier O'Phinney</name>
      <email>contact@mwop.net</email>
      <uri>https://mwop.net</uri>
    </author>
    <content xmlns:xhtml="http://www.w3.org/1999/xhtml" type="xhtml">
      <xhtml:div xmlns:xhtml="http://www.w3.org/1999/xhtml"><xhtml:p>I was recently building something that was taking date input
from an HTML form field, and casting it to a PHP
<xhtml:code>DateTimeImmutable</xhtml:code>. I was then comparing that to
another date, and got thrown off during testing when I compared the
resulting instance to <xhtml:code>new DateTimeImmutable('today')</xhtml:code>;
the instances were not considered equal.</xhtml:p>
<xhtml:p>To recreate the conditions, you can try the following:</xhtml:p>
<xhtml:pre><xhtml:code class="language-php hljs php" data-lang="php">$date     = <xhtml:span class="hljs-string">'2016-06-16'</xhtml:span>;
$fromForm = DateTimeImmutable::createFromFormat(<xhtml:span class="hljs-string">'Y-m-d'</xhtml:span>, $date);
$today    = <xhtml:span class="hljs-keyword">new</xhtml:span> DateTimeImmutable(<xhtml:span class="hljs-string">'today'</xhtml:span>);
<xhtml:span class="hljs-keyword">echo</xhtml:span> $fromForm == $today ? <xhtml:span class="hljs-string">'Equal'</xhtml:span> : <xhtml:span class="hljs-string">'Not equal'</xhtml:span>; <xhtml:span class="hljs-comment">// outputs "Not equal"</xhtml:span>
</xhtml:code></xhtml:pre>
<xhtml:p>What's happening? Well, if you were to echo the results of each
of <xhtml:code>$fromForm-&gt;format('c')</xhtml:code> and
<xhtml:code>$today-&gt;format('c')</xhtml:code>, the difference is clear: the
<xhtml:code>$fromForm</xhtml:code> value includes the <xhtml:em>time</xhtml:em> when the
instance was created, while <xhtml:code>$today</xhtml:code> has the time set to
midnight.</xhtml:p>
<xhtml:p>So, how do you zero out the time when using
<xhtml:code>createFromFormat()</xhtml:code>?</xhtml:p>
<xhtml:p>It turns out that one of the format characters you can use is
the <xhtml:code>|</xhtml:code> operator. When you include this at the end of
your format string, any fields not included in the format are
zero'ed out:</xhtml:p>
<xhtml:pre><xhtml:code class="language-php hljs php" data-lang="php">$fromForm = DateTimeImmutable::createFromFormat(<xhtml:span class="hljs-string">'Y-m-d|'</xhtml:span>);
</xhtml:code></xhtml:pre>
<xhtml:hr/>
<xhtml:h4>Reference</xhtml:h4>
<xhtml:ul>
<xhtml:li><xhtml:a href="https://www.php.net/manual/en/datetimeimmutable.createfromformat.php#datetimeimmutable.createfromformat.parameters">
DateTimeImmutable::createFromFormat() Parameters</xhtml:a></xhtml:li>
</xhtml:ul>
<xhtml:div class="h-entry"><xhtml:img class="u-photo photo" width="50" src="https://avatars0.githubusercontent.com/u/25943?v=3&amp;u=79dd2ea1d4d8855944715d09ee4c86215027fa80&amp;s=140" alt="matthew"/> <xhtml: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</xhtml:a> was
originally published <xhtml:time class="dt-published" datetime="2026-04-16T10:30:00-05:00">16 April 2026</xhtml:time> on <xhtml:a href="https://mwop.net">https://mwop.net</xhtml:a> by <xhtml:a rel="author" class="p-author" href="https://mwop.net">Matthew Weier
O'Phinney</xhtml:a>.</xhtml:div>
</xhtml:div>
    </content>
  </entry>
</feed>
