"strtotime('tomorrow') - time()" OR "strtotime('tomorrow') - strtotime('now')" will not give the correct time until upcoming midnight

StackOverflow https://stackoverflow.com/questions/22393216

  •  14-06-2023
  •  | 
  •  

Pergunta

I'm trying to do a simple countdown, but it's giving the wrong time:

$time_seconds_view = strtotime('tomorrow') - time(); echo date("H" . " \h\o\u\\r\s\, " . "i" . " \m\i\\n\u\\t\\e\s\, \a\\n\d " . "s" . " \s\\e\c\o\\n\d\s", $time_seconds_view);

is spitting out 00 hours, 30 minutes, and 00 seconds

even though date_default_timezone_set($this->session->userdata('timezone')); is set to America/Phoenix

it should be spitting out 07 hours, 30 minutes, and 00 seconds

it's as if PHP is not taking account that I've changed the default timezone.

Anything I'm doing wrong? Thanks for your help!

Foi útil?

Solução

Use DateTime. It's much better for date math.

$tomorrow = new DateTime('tomorrow');
$now = new DateTime();
$diff = $tomorrow->diff($now);
echo $diff->format("%h hours, %i minutes, %s seconds");
// 7 hours, 12 minutes, 53 seconds

See in action

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top