php 遇到格式不正确的数值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6136430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
A non well formed numeric value encountered
提问by Deviland
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
我有一个表单将两个日期(开始和结束)传递给一个 PHP 脚本,该脚本会将这些日期添加到数据库中。我在验证这一点时遇到了问题。我不断收到以下错误
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?
但是,当我按照许多站点的建议使用strtotime()函数时,我得到 1/1/1970 的 unix 时间戳日期。任何想法如何获得正确的日期?
回答by DChaplin
Because you are passing a string as the second argument to the date function, which should be an integer.
因为您将一个字符串作为第二个参数传递给 date 函数,它应该是一个整数。
string date ( string $format [, int $timestamp = time() ] )
字符串日期(字符串 $format [, int $timestamp = time() ] )
Try strtotimewhich will Parse about any English textual datetime description into a Unix timestamp(integer):
尝试strtotime,它会将任何英文文本日期时间描述解析为 Unix 时间戳(整数):
date("d", strtotime($_GET['start_date']));
回答by Wesley van Opdorp
$_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 strtotimeor use combination of explode/mktime.
$_GET['start_date']
不是数字是我的赌注,但不支持的日期格式strtotime
。您需要将日期重新格式化为适用于strtotime 的格式或使用组合expand/ mktime。
I could add you an example if you'd be kind enough to post the format you currently receive.
如果您愿意发布您当前收到的格式,我可以为您添加一个示例。
回答by Faisal
Simply you can solve this issue using strtotime()
function.
只需使用strtotime()
函数即可解决此问题。
date("d", strtotime($_GET['start_date']));
回答by spidersilk
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.
我遇到了同样的情况(在我的情况下,在 Drupal 视图中的自定义 PHP 字段中有一个日期值),对我有用的是使用 intval 而不是 strtotime 将值转换为整数 - 因为它基本上是一个时间戳,但以字符串而不是整数的形式。显然,并不是每个人都如此,但这可能值得一试。
回答by Vladimir Salguero
This error occurs when you perform calculations with variables that use letters combined with numbers (alphanumeric), for example 24kb, 886ab ...
当您对使用字母与数字(字母数字)组合的变量执行计算时会发生此错误,例如 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:
解决方案:该intval()
函数使用字母数字数据提取变量的整数值,并创建一个具有相同值但使用intval()
函数转换为整数的新变量。这是代码:
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);
}
回答by Tushar Walzade
This helped me a lot -
这对我帮助很大 -
$new_date = date_format(date_create($old_date), 'Y-m-d');
回答by Parris Varney
This is an old question, but there is another subtle way this message can happen. It's explained pretty well here, in the docs.
这是一个古老的问题,但这条消息还有另一种微妙的方式发生。它在 docs 中的here解释得很好。
Imagine this scenerio:
想象一下这个场景:
try {
// code that triggers a pdo exception
} catch (Exception $e) {
throw new MyCustomExceptionHandler($e);
}
And MyCustomExceptionHandler
is defined roughly like:
并MyCustomExceptionHandler
大致定义为:
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.
这实际上会在自定义异常处理程序中触发一个新的异常,因为Exception
该类期望其构造函数中的第二个参数为数字,但PDOException
可能已将 的返回类型动态更改$e->getCode()
为字符串。
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();
}
}
回答by Arghadeeph Halder
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.
如果错误发生在任何计算时,请仔细检查这些值是否不包含任何逗号 (,)。值只能是整数/浮点格式。
回答by AlanP
You need to set the time zone using date_default_timezone_set().
您需要使用 date_default_timezone_set() 设置时区。
回答by Nuno
Passing a string isn't necessarily a problem, if it only contains digits.
传递字符串不一定是问题,如果它只包含数字。
One other reason this can happen is if you have a space before or after. E.g. "1557399276 "
发生这种情况的另一个原因是之前或之后是否有空格。例如“1557399276”