A non well formed numeric value encountered
PhpValidationDateTimePhp Problem Overview
I have a form that passes two dates (start and finish) to a PHP script that will add those to a DB. I am having problems validating this. I keep getting the following errors
A non well formed numeric value encountered
This is when I use the following
date("d",$_GET['start_date']);
But when I use the strtotime() function as advised by many sites I get a unix timestamp date of 1/1/1970. Any ideas how I can get the correct date?
Php Solutions
Solution 1 - Php
Because you are passing a string as the second argument to the date function, which should be an integer.
> string date ( string $format [, int $timestamp = time() ] )
Try strtotime which will Parse about any English textual datetime description into a Unix timestamp (integer):
date("d", strtotime($_GET['start_date']));
Solution 2 - Php
This error occurs when you perform calculations with variables that use letters combined with numbers (alphanumeric), for example 24kb, 886ab ...
I had the error in the following function
function get_config_bytes($val) {
$val = trim($val);
$last = strtolower($val[strlen($val)-1]);
switch($last) {
case 'g':
$val *= 1024;
case 'm':
$val *= 1024;
case 'k':
$val *= 1024;
}
return $this->fix_integer_overflow($val);
}
The application uploads images but it didn't work, it showed the following warning:
Solution: The intval()
function extracts the integer value of a variable with alphanumeric data and creates a new variable with the same value but converted to an integer with the intval()
function. Here is the code:
function get_config_bytes($val) {
$val = trim($val);
$last = strtolower($val[strlen($val)-1]);
$intval = intval(trim($val));
switch($last) {
case 'g':
$intval *= 1024;
case 'm':
$intval *= 1024;
case 'k':
$intval *= 1024;
}
return $this->fix_integer_overflow($intval);
}
The function fix_integer_overflow
// Fix for overflowing signed 32 bit integers,
// works for sizes up to 2^32-1 bytes (4 GiB - 1):
protected function fix_integer_overflow($size) {
if ($size < 0) {
$size += 2.0 * (PHP_INT_MAX + 1);
}
return $size;
}
Solution 3 - Php
$_GET['start_date']
is not numeric is my bet, but an date format not supported by strtotime
. You will need to re-format the date to a workable format for strtotime or use combination of explode/mktime.
I could add you an example if you'd be kind enough to post the format you currently receive.
Solution 4 - Php
I ran into this same situation (in my case with a date value in a custom PHP field in a Drupal view), and what worked for me was using intval instead of strtotime to turn the value into an integer - because it basically was a timestamp, but in the form of a string rather than an integer. Obviously that won't be the case for everyone, but it might be worth a try.
Solution 5 - Php
This helped me a lot -
$new_date = date_format(date_create($old_date), 'Y-m-d');
> Here, date_create()
provides you a date object for a given date & date_format()
will set it in a given format.
for example,
<?php
$date = date_create("13-02-2013"); // DateTime Object ( [date] => 2013-02-13 00:00:00.000000 [timezone_type] => 3 [timezone] => America/New_York )
echo date_format($date,"Y-m-d"); // 2013-02-13
?>
Solution 6 - Php
This is an old question, but there is another subtle way this message can happen. It's explained pretty well here, in the docs.
Imagine this scenerio:
try {
// code that triggers a pdo exception
} catch (Exception $e) {
throw new MyCustomExceptionHandler($e);
}
And MyCustomExceptionHandler
is defined roughly like:
class MyCustomExceptionHandler extends Exception {
public function __construct($e) {
parent::__construct($e->getMessage(), $e->getCode());
}
}
This will actually trigger a new exception in the custom exception handler because the Exception
class is expecting a number for the second parameter in its constructor, but PDOException
might have dynamically changed the return type of $e->getCode()
to a string.
A workaround for this would be to define you custom exception handler like:
class MyCustomExceptionHandler extends Exception {
public function __construct($e) {
parent::__construct($e->getMessage());
$this->code = $e->getCode();
}
}
Solution 7 - Php
If the error is at the time of any calculation, double check that the values does not contains any comma(,). Values must be only in integer/ float format.
Solution 8 - Php
if $_GET['start_date'] is a string then convert it in integer or double to deal numerically.
$int = (int) $_GET['start_date']; //Integer
$double = (double) $_GET['start_date']; //It takes in floating value with 2 digits
Solution 9 - Php
in the name of the universe programmer
in my case i receive this error >"Notice: A non well formed numeric value encountered in"
my code with above error:
$end_suggest_time = $rowone['start_suggest_time'] + (24 * 3600);
when i add (int)
in the previous of $rowone['start_suggest_time']
I don't receive this error
>"Notice: A non well formed numeric value encountered in"
$end_suggest_time = (int) $rowone['start_suggest_time'] + (24 * 3600);
Solution 10 - Php
You need to set the time zone using date_default_timezone_set().