How to get the time hh:mm:ss of any state starting by seconds from midnight without changing date_default_timezone_set regardless of the state I am in

For some reason in my code at the top of the page there’s date_default_timezone_set('America/Belize'); and I don’t want to change it.

For getting the current London seconds from midnight (or any other timezone) regardless of the state I am in I’ve tryied with a small function (just a little modded) found here on StackOverflow:

function seconds_from_midnight($iana_timezone = 'Europe/London'){
    $time_now = time(); //UNIX TIMESTAMP (IT'S THE SAME EVERYWHERE)
    $date = new DateTime(); 
    $date->setTimestamp( $time_now );
    $date->setTimezone(new DateTimeZone($iana_timezone));
    $date->modify('today midnight');
    $midnight_time = $date->getTimestamp();
    $seconds_since_midnight = $time_now - $midnight_time;
    return $seconds_since_midnight;
}

I think this first part it’s correct because it use a unix timestamp (UTC-0 starting from 1970).

Then I check the time in London with (this part is completely wrong):

$r = seconds_from_midnight();
echo 'Time London now: '.date('H:i:s', $r).'<br>'; //How to do this for each state?

[It should be the correct time if you are a person in London looking at the clock right now]

Now see the problem that occurs:

date it’s refers to the server, so gives me back a wrong time (I don’t know why but I think the reason it’s date_default_timezone_set at the beginning of the page, that I don’t want to change).

Please note: The timezone given by the variable $iana_timezone may be different from Europe/London.

I don’t live in America/Belize and the PHP server it is in an other state (I don’t know which one and honestly I don’t care).

So, how to get the time from midnight in London (or in another state) in this conditions?

Your function returns the correct result, but your indirect check is not quite correct.

date('H:i:s', $r) does not contain time zone information, so it uses default time zone (America/Belize). But, to begin with, you’re using a relative amount of seconds as an absolute Unix time. You are basically taking Unix Epoch as starting point, extracting a time from 1st January 1970 and printing it in current default time zone. Unix Epoch in Belize didn’t happen even near midnight, so the time printed is way off track.

Even if you change default time zone temporarily for this task, London is currently using daylight saving time, so it’ll be 1 hour off. It’ll work by pure chance after last Sunday in October and will break again in March.

If you need to handle different time zones, use DateTime all the way through.

function seconds_from_midnight(string $iana_timezone = 'Europe/London'): int
{
    $timeZone = new DateTimeZone($iana_timezone);
    $now = new DateTime();
    $midnight = new DateTime('today midnight', $timeZone);
    return $now->getTimestamp() - $midnight->getTimestamp();
}

$seconds = seconds_from_midnight();

$londonTimeZone = new DateTimeZone('Europe/London');
$midnightInLondon = new DateTimeImmutable('today midnight', $londonTimeZone);
$nowInLondon = new DateTimeImmutable(timezone: $londonTimeZone);
$sum = $midnightInLondon->modify("+$seconds seconds");

var_dump($seconds, $midnightInLondon, $nowInLondon, $sum);

5

function seconds_from_midnight($iana_timezone = 'Europe/London') {
    
    $time_now = time(); 
    
    
    $date = new DateTime();
    $date->setTimestamp($time_now);
    $date->setTimezone(new DateTimeZone($iana_timezone));
    
    
    $midnight = clone $date;
    $midnight->setTime(0, 0, 0); // Midnight (00:00:00)

    
    $seconds_since_midnight = $date->getTimestamp() - $midnight->getTimestamp();
    
    return $seconds_since_midnight;
}

$seconds = seconds_from_midnight('Europe/London');
echo "Seconds from midnight in London: " . $seconds . "<br>";

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật