php:遇到非格式的数值


php : A non well formed numeric value encountered

我试图得到用户的年龄,我得到正确的输出,但在我的HTML页面警告

错误:

A PHP Error was encountered
Severity: Notice
Message: A non well formed numeric value encountered
Filename: controllers/users.php
Line Number: 45

我使用的代码是:

$now = time();
$data['dob'] =  strftime('%Y', $now)-strftime('%Y',$query['dob']);   //it should be showig user age

上面显示的错误可能是什么原因,请帮助

strftime返回一个字符串,您必须在进行减法之前将其转换为数字。

编辑:看到下面的评论,你不能减法,因为dobtime()返回的格式不同。所以,按照上面的段落继续。

EDIT2:最干净的方法减去两个日期是DateTime

我将字符串转换为int,并减去该值以得到我的输出

$userdob=(int)$query['dob']; $data['dob'] = strftime('%Y', $now)- strftime('%Y',$userdob);

这可能是因为$query['dob']包含的不是整数。

strftime()期望它的第二个参数是一个时间戳(也就是说,一个unix时间戳,一个表示经过epoch的秒数的整数)。如果让我猜,我猜你的$query['dob']包含某种字符串