"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
  •  | 
  •  

문제

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!

도움이 되었습니까?

해결책

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top