Jump to content

Archived

This topic is now archived and is closed to further replies.

EP

[SOLVED] A non well formed numeric value encountered

Recommended Posts

Hello, I need a little help here.  I can't figure out what seems to be the problem with my code here.  Everytime my script executes, I get a log full of this type stuff over and over again:

 

[sat May 30 07:23:36 2009] [error] [client 127.0.0.1] PHP Notice:  A non well formed numeric value encountered in C:\\Program Files\\Apache Software Foundation\\Apache2.2\\htdocs\\topsongs.php on line 234

 

Here is an example of the code I am having a problem with:

 


$now = date('Y-m-d H:i:s');
$now2 = strtotime($now);
$oneweek = strtotime("+1 week", $now);
$oneweekago = $now2-$oneweek;

$timeplayedaddstr = strtotime($timeplayedadd);


if($timeplayedaddstr>$oneweekago){
$topsongs[$arraytitle]['playsthisweek'] = $topsongs[$arraytitle]['playsthisweek'] + 1;
};


 

Hopefully that's enough code to give you an idea of what I am trying to do.

 

$timeplayedadd is a variable which is defined in each entry in my array.  It is time and date of the entry in the default mysql format.

 

The line that generates the "non well formed numeric value" error in the log is:

 

$oneweek = strtotime("+1 week", $now);

 

Any idea of how to fix this?

 

Share this post


Link to post
Share on other sites

strtotime() expects a integer as second parameter; you're prodiving a string ($now). You can simply omit the second parameter, since you're using the current timestamp:

 

$oneweek = strtotime("+1 week");

Share this post


Link to post
Share on other sites

I tried that already, it's not working for me.

 

 

Here, try this:

 

<?php

$now = date('Y-m-d H:i:s');
$now2 = strtotime($now);
$oneweek = strtotime("+1 week",$now);
$oneweek2 = strtotime("+1 week");
$oneweekago = $now2-$oneweek;
$oneweekagosearch = date('Y-m-d H:i:s',$oneweekago);

echo "now: " . $now . "<BR>\n";
echo "now2: " . $now2 . "<BR>\n";
echo "oneweek: " . $oneweek . "<BR>\n";
echo "oneweek2: " . $oneweek2 . "<BR>\n";
echo "oneweekago: " . $oneweekago . "<BR>\n";
echo "oneweekagosearch: " . $oneweekagosearch . "<BR>\n";

?>

 

Here's the output I got from that:

 

now: 2009-05-30 08:25:23
now2: 1243697123
oneweek: 606809
oneweek2: 1244301923
oneweekago: 1243090314
oneweekagosearch: 2009-05-23 07:51:54

 

And I still got a line in the error.log that says "A non well formed numeric value encountered"

 

I need the output from the $oneweek line to be a certain way or it throws the rest of the script off.  Maybe there is something else I can use for that line besides strtotime?

Share this post


Link to post
Share on other sites

You still get the error because you didn't follow my advice. And I don't think you need all those values? To get the timestamp from one week ago:

 

$oneweekago = strtotime('-1 week');

Your current code may work because strtotime() defaults to using the Unix Epoch (timestamp 0) when you feed it a wrongful second parameter.

Share this post


Link to post
Share on other sites

Ok, I got it to work.

 


<?php

$oneweek = date('Y-m-d H:i:s', strtotime('-1 week'));
$oneweekago = strtotime($oneweek);

echo "oneweek: " . $oneweek . "<BR>\n";
echo "oneweekago: " . $oneweekago . "<BR>\n";


?>

 

Works just fine.

 

Thanks for the help. :D

Share this post


Link to post
Share on other sites

You're welcome :)

 

I guess you could improve your naming of the variables to make it easier for everyone; e.g.

 

<?php
$lastweek_timestamp = strtotime('-1 week');
$lastweek_str = date('Y-m-d H:i:s', $lastweek_timestamp);
?>

As you can see, I also reused the timestamp instead of running strtotime() twice.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.