Understanding "A non well formed numeric value encountered" Error in PHP
When attempting to validate dates passed from a form, you may encounter the error "A non well formed numeric value encountered." This error indicates that you're trying to use a value that's not a well-formed number.
In your specific case, you're getting this error while trying to use the date() function with a GET parameter representing a start date. The issue arises because this parameter is being passed as a string in the format "2020-10-10" or "2020/10/10," which is not a valid numeric value for the date() function.
Resolving the Issue
To resolve this issue, you need to convert the date parameter to a numeric value before passing it to the date() function. You can use the strtotime() function to convert the string date representation to a number of seconds. Here's the corrected code:
date("d", strtotime($_GET['start_date']));
Additional Considerations
It's crucial to understand the root cause of this error type. Whenever you encounter such an error, you should first investigate the problem value using var_dump(). This will help you determine the following:
Never resort to blindly casting the problem value to a number, as this will conceal the actual problem and potentially lead to incorrect results.
The above is the detailed content of Why am I getting a 'A non well formed numeric value encountered' error when using PHP's `date()` function with a GET parameter?. For more information, please follow other related articles on the PHP Chinese website!