0

I am trying to bring my old website back to life and I found that my code is pretty outdated. I have updated a lot so far without much headache, but I can't seem to get past this error. (Its been a while since I've written any PHP.)

Error:

PHP Notice: A non well formed numeric value encountered

Code:

if(strtotime('-1 day', date('Y-m-d')) == $lastVisit) {

    $consecutive = $consecutive + 1;
    $mysqli->query("UPDATE login SET log_in_time=NOW(),consecutive='".$mysqli->real_escape_string($consecutive)."' WHERE name='".$mysqli->real_escape_string($name)."'");

}

It seems to be the very first line but can't seem to figure it out.

Unmitigated
  • 76,500
  • 11
  • 62
  • 80
SDZ
  • 726
  • 2
  • 8
  • 21
  • It is a different value for each user – SDZ Jul 11 '20 at 18:42
  • Yes that's true – SDZ Jul 11 '20 at 18:43
  • It's an int.... – SDZ Jul 11 '20 at 18:45
  • 2
    Oh, then likely `date('Y-m-d')` causes your issue. Do `strtotime('-1 day', time())` because second parameter must be `The timestamp which is used as a base for the calculation of relative dates.` but `date` returns `string` not `timestamp`. You could run `strtotime` on it but that's a bit repetitive. – user3783243 Jul 11 '20 at 18:45
  • Does `real_escape_string()` work on ints? Just wondering. .... But i was able to reproduce your error. It is indeed *the very first line*. – GetSet Jul 11 '20 at 18:46
  • @GetSet It only escapes. Won't modify value unless it had a quote in it, then would be escaped. – user3783243 Jul 11 '20 at 18:48
  • 1
    If OP uses `time()` then likely one day minus that will never equal `$lastVisit` unless it was also the same second, minute, and hour of the day. ... @user3783243 – GetSet Jul 11 '20 at 18:56
  • @GetSet Oh, yea, good point. – user3783243 Jul 11 '20 at 19:01
  • You can likely do this all in SQL. presuming `$lastVisit` comes from the db. – user3783243 Jul 11 '20 at 19:01
  • Please read: https://stackoverflow.com/questions/7537377/how-to-include-a-php-variable-inside-a-mysql-statement – Dharman Jul 11 '20 at 20:08
  • I really do not recommend using `strtotime`. This kind of thing should be done either in SQL or using `DateTime` class – Dharman Jul 11 '20 at 20:11

2 Answers2

1

Like the PHP documentation points out, strtotime() expects the second parameter to be ìnt.

strtotime ( string $time [, int $now = time() ] ) : int

in your code, you're giving a string because date() has the return type string.

See this example on how to check if given $lastVisit was yesterday:

<?php
$lastVisit = '2020-07-11 14:48:16'; // example value

$lastVisitTimestamp = strtotime($lastVisit); // convert timestamp to int
$lastVisitDate = date("Ymd", $lastVisitTimestamp); // convert $lastVisit to Ymd-String representing 
$yesterdayDate = date("Ymd", strtotime('-1 day', time())); // remove one day from current time and convert it to comparable string

// compares two strings f.e "20207010 === 20200711" to check if $lastVisit was yesterday
if($yesterdayDate === $lastVisitDate) {
    echo "yeah! last visit was yesterday :)";
} else {
    echo "no :(";
}
?>
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
Amacado
  • 630
  • 5
  • 20
  • 1
    hmm. could you give an example? – SDZ Jul 11 '20 at 18:56
  • Was wondering the same on the *could you give an example*. Use of `time()` by the way without params, will likely introduce an unintended logical error – GetSet Jul 11 '20 at 18:59
  • Please give an example of `$lastVisit` and tell us what is the logical task of this condition? What do you want to "find out"? Do you want to check if the $lastVisit was yesterday? – Amacado Jul 11 '20 at 19:01
  • Sure, 2020-07-11 14:48:16 – SDZ Jul 11 '20 at 19:04
  • 1
    It starting to look like either your code never worked or never logically worked @SamiDzHamida *even* in previous PHP versions. ... basing this comment on your example of `$lastVisit` – GetSet Jul 11 '20 at 19:08
  • It definitely worked, I'm not the strongest coder but this was done many many years ago – SDZ Jul 11 '20 at 19:09
  • 1
    Sorry my mistake! $lastVisit = 1594492074 – SDZ Jul 11 '20 at 19:11
  • @SamiDzHamida In that case you should make `Y-m-d` format out of `$lastVisit` too. – noam Jul 11 '20 at 19:12
  • I updated my answer with an example on how to check if $lastVisit was yesterday.. – Amacado Jul 11 '20 at 19:14
  • 1
    @Amacado, the variable name `$yesterdayDate` is a little misleading - we don't know if this is actually yesterday unless the `if` condition is met. – noam Jul 11 '20 at 19:18
  • @noam no. $yesterdayDate is always yesterday. Its time() (today) minus 1 day. – Amacado Jul 11 '20 at 19:19
  • Agree with @noam, What you have semantically reads like "if yesterday is equal to the day before the last visit". It works but just makes the code misleading to OP intent. Unless you just like puzzles in how your choose identifiers. – GetSet Jul 11 '20 at 19:25
  • @GetSet you're partly right, had a logical error in my code. I don't need to remove one day from the $lastVisit. Updated my answer. But $yesterdayDate will be always yesterday. – Amacado Jul 11 '20 at 19:34
  • Makes sense @Amacado – GetSet Jul 11 '20 at 19:51
1

As was mentioned before, strtotime() expects an integer as the second argument.

Here is an alternative:

$lastVisitDate = (new \DateTime($lastVisit))->format('Y-m-d');
$thisDate = date('Y-m-d', time() - 60 * 60 * 24);

if ($lastVisitDate === $thisDate) {
…
}

Edit: added a timestamp to date().

noam
  • 538
  • 5
  • 19
  • `$thisDate` is missleading, since you subtract a day it's actually not `$thisDate` but instead `$yesterdayDate` (see my answer). – Amacado Jul 11 '20 at 19:58