Date error: "non well formed numeric value encountered"

Hello everyone,

Greetings from Brazil! My local test server is running PHP 7.2.24 and I’m getting the following error:

"Notice: A non well formed numeric value encountered in /opt/lampp/htdocs/portal/restrict/course.php on line 399"

The error occurs in this specific line of code:

$endcycle = date(‘m-d’, ‘05-20’);

I’m wondering if this is a bug with PHP7, because this error doesn’t happen on my remote server, which is runnung PHP 5.2.6

Thank you for your time and help. I really appreciate it.

Stay home, stay safe.

The second parameter of the date() function is a unix timestamp, an integer. Supplying it the string ‘05-20’, would result in an integer value of 5, which would be 5 seconds after the unix epoch and the result of that date() call would give you the month and day that unix epoch occurred on. This is pretty meaningless from a programming standpoint. That you are not getting an error message on your remote server just means that php’s error related settings are not set up to show you all errors.

What overall goal is this line of code trying to accomplish? If you are trying to get the month and day value of a string date, you can just do that directly using php’s substr() function.

I found a solution!!!

All I had to do was convert the number from string to time and then replace the dash (-) separator with a forward slash (/):

$endcycle= date(‘m-d’, strtotime(str_replace(’-’,’/’, ‘05-20’)));

My code now works beautifully! I hope this helps anyone having the same kind of problem. Best regards to all! :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service