-2

I want to get the difference time between Now and today midnight but I get a negative number. Why?

  echo strtotime('today midnight') - strtotime("now");

1 Answers1

1

As mentioned in ceeyajoz's comment, midnight is relatively unclear, see this reference link.

As for your question, consider this eval.in script.

Yesterday midnight is not the same as today midnight, which is sooner than now. It seems that midnight means the midnight that happened at the beginning of the present day. Considering that, it's understandable that you get a negative integer, since midnight already happened.

As mentioned in yet another useful comment:

@user3066977 If you want the time left until midnight, do strtotime('tomorrow midnight') - time() or strtotime('tomorrow 00:00:00') - time()

  • the same problem echo strtotime("yesterday midnight") - strtotime("now"); –  Jun 13 '18 at 00:49
  • i do SELECT * FROM tasks WHERE task_accomplish_date < strtotime("yesterday midnight") and i don't get any result –  Jun 13 '18 at 00:52
  • @user3066977 in all honesty, I don't know by heart all the string time possibilities, maybe search for the one you need. The question was "why is it a negative integer", the answer is "because midnight is farther than now". – Félix Adriyel Gagnon-Grenier Jun 13 '18 at 00:56
  • so there is no difference between yesterday midnight and today midnight ? –  Jun 13 '18 at 00:58
  • 1
    @user3066977 If you want the time left until midnight, do `strtotime('tomorrow midnight') - time()` or `strtotime('tomorrow 00:00:00') - time()` – ceejayoz Jun 13 '18 at 00:59