Home > Backend Development > PHP Tutorial > Why am I getting a 'A non well formed numeric value encountered' error when using PHP's `date()` function with a GET parameter?

Why am I getting a 'A non well formed numeric value encountered' error when using PHP's `date()` function with a GET parameter?

Susan Sarandon
Release: 2024-12-04 05:10:14
Original
660 people have browsed it

Why am I getting a

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']));
Copy after login

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:

  • If you're accessing an incorrect variable or data structure.
  • If the value can be converted to a valid numeric format (e.g., using strtotime()).
  • If the source of the problem data requires validation or logic adjustments.

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template