--- 
canonical: 'https://mwop.net/blog/2026-04-16-php-create-from-format-reset.html'
title: 'PHP DateTime&shy;Im&shy;mut&shy;able::&shy;create&shy;From&shy;Format Reset Character'
author: "[Matthew Weier O'Phinney](https://mwop.net)"
created: '2026-04-16T10:30:00-05:00'
updated: '2026-04-16T10:30:00-05:00'
tags:
  - pcre
  - php
  - til

---
I was recently building something that was taking date input from an HTML form field, and casting it to a PHP `DateTimeImmutable`. I was then comparing that to another date, and got thrown off during testing when I compared the resulting instance to `new DateTimeImmutable('today')`; the instances were not considered equal.

















To recreate the conditions, you can try the following:

```php
$date     = '2016-06-16';
$fromForm = DateTimeImmutable::createFromFormat('Y-m-d', $date);
$today    = new DateTimeImmutable('today');
echo $fromForm == $today ? 'Equal' : 'Not equal'; // outputs "Not equal"
```

What's happening? Well, if you were to echo the results of each of `$fromForm->format('c')` and `$today->format('c')`, the difference is clear: the `$fromForm` value includes the _time_ when the instance was created, while `$today` has the time set to midnight.

So, how do you zero out the time when using `createFromFormat()`?

It turns out that one of the format characters you can use is the `|` operator. When you include this at the end of your format string, any fields not included in the format are zero'ed out:

```php
$fromForm = DateTimeImmutable::createFromFormat('Y-m-d|');
```

---

#### Reference

- [DateTime&shy;Im&shy;mut&shy;able::&shy;create&shy;From&shy;Format() Parameters](https://www.php.net/manual/en/datetimeimmutable.createfromformat.php#datetimeimmutable.createfromformat.parameters)
