Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

date() fails when you pass it a date string from a form

ErrorA non well formed numeric value encountered

Short answer

The second argument of date() is an integer Unix timestamp, not a date string. A form value such as 2026-11-17 is a string, so PHP treats it as a number that is not well formed. Convert it first with strtotime() or DateTimeImmutable, check that the conversion did not return false, and only then format it.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print what the form really sends and what date() is given. A string like 17/11/2026 is the usual surprise.

    var_dump($_POST['date'], strtotime($_POST['date']));
  2. 2Match what you see, then jump to its fix

    1. 1the warning or error names date() and the value is something like 2026-11-17A date string is passed where date() wants a timestampFix
    2. 2strtotime() returns false, and date() then gets a value you did not intendThe text is not in a format strtotime() acceptsFix
    3. 331/02/2026 is accepted and shows a date in MarchAn impossible date is silently shifted instead of rejectedFix

What does the error look like?

  • A non well formed numeric value encountered

Cause 1 of 3

A date string is passed where date() wants a timestamp

1Check

The signature is date(string $format, ?int $timestamp = null). A numeric string is fine, a date string is not. In PHP 8, a leading-numeric string such as 2026-11-17 raises a warning, and a non-numeric string throws a TypeError.

var_dump(is_numeric($_POST['date']));

2Fix

Turn the string into a timestamp with strtotime(), which returns false when it cannot read the text, and test for false.

<?php
$ts = strtotime($_POST['date'] ?? '');
if ($ts === false) {
    exit('Invalid date');
}
echo date('d M Y', $ts);

3Verify

Post a valid date and an invalid one; the first prints a date, the second prints Invalid date.

curl -d "date=2026-11-17" http://localhost/test.php
curl -d "date=hello" http://localhost/test.php

Confirmed by php.net

Did this fix it?

Cause 2 of 3

The text is not in a format strtotime() accepts

1Check

strtotime() returns false when the text is not a valid date format. Day-first text such as 17/11/2026 is a typical case. The manual lists the accepted formats.

var_dump(strtotime('17/11/2026'));
var_dump(strtotime('2026-11-17'));

2Fix

When the format is fixed (a datepicker, for example), parse it with that exact format using DateTimeImmutable::createFromFormat(), which returns false on failure, and read getLastErrors() for the reason.

<?php
$d = DateTimeImmutable::createFromFormat('d/m/Y', $_POST['date'] ?? '');
if ($d === false) {
    exit('Invalid date');
}
echo $d->format('d M Y');

3Verify

Post 17/11/2026 and read 17 Nov 2026 back.

curl -d "date=17/11/2026" http://localhost/test.php

Confirmed by php.net

Did this fix it?

Cause 3 of 3

An impossible date is silently shifted instead of rejected

1Check

createFromFormat() lets overflowing dates through with a warning in getLastErrors(), not a failure.

$d = DateTimeImmutable::createFromFormat('d/m/Y', '31/02/2026');
print_r(DateTimeImmutable::getLastErrors());

2Fix

Compare the formatted result to the input, or check the warning count, before you use the date.

<?php
$input = $_POST['date'] ?? '';
$d = DateTimeImmutable::createFromFormat('d/m/Y', $input);
if (!$d || $d->format('d/m/Y') !== $input) {
    exit('Invalid date');
}

3Verify

31/02/2026 is rejected, 28/02/2026 is accepted.

curl -d "date=31/02/2026" http://localhost/test.php

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Never pass request input straight to date(); convert and validate it first.
  • Prefer DateTimeImmutable over timestamps for anything that carries a timezone, as the manual advises.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.