date() fails when you pass it a date string from a form
A non well formed numeric value encounteredShort 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.
Fix it fast
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']));2Match what you see, then jump to its fix
- 1
the warning or error names date() and the value is something like 2026-11-17A date string is passed where date() wants a timestampFix - 2
strtotime() returns false, and date() then gets a value you did not intendThe text is not in a format strtotime() acceptsFix - 3
31/02/2026 is accepted and shows a date in MarchAn impossible date is silently shifted instead of rejectedFix
- 1
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.phpConfirmed by php.net
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.phpConfirmed by php.net
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.phpConfirmed by php.net
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
Warning: A non-numeric value encountered (and Unsupported operand types: string + int)
Fatal error: Uncaught TypeError: Unsupported operand types: string * int
You used a string in arithmetic that is not a clean number.
3 causes, each with a fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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 fixBack end
Apache returns 500 with Premature end of script headers
500 Internal Server Error
Apache ran your script, but the script printed no valid HTTP header before its output.
3 causes, each with a fixBack end
Apache returns 503 or 504 because PHP-FPM did not answer in time
Error dispatching request
Apache sent the request to PHP-FPM and gave up waiting.
3 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fix