Jump to content

Check time difference between 2 different Time Zones (and with DST)


Recommended Posts

Hi all guys,

my PHP scripts on GoDaddy DB save date/time in UTC format 

Data on server db: 2023-08-09 13:00:00

_nowcalc():   2023-08-09 15:00:00 (I'm in Italy, CET + DST)

I need to check date difference between server date (let's call it $utcStoredTimestamp) and my PC time.

In this case difference must return 0: I run the php script now on GoDaddy and it saves me a row on DB with this date; on my PC i read this other date)

I use the following code to test it

#include <Date.au3>
#include <Array.au3>

; Get the time zone information
Local $tTimeZone = _Date_Time_GetTimeZoneInformation()
Local $daylightSavingsSetting = $tTimeZone[0]
Local $timeZoneBias = $tTimeZone[1]
;_ArrayDisplay($tTimeZone)

; UTC timestamp retrieved from the database
$utcStoredTimestamp = "2023-08-09 13:08:00"  ; UTC

; Convert UTC stored timestamp to local time considering the time zone bias
$localStoredTimestamp = _DateAdd("n", -$timeZoneBias, $utcStoredTimestamp)

; Check if daylight savings time is in effect
If $daylightSavingsSetting = 2 Then
    $daylightSavingsBias = $tTimeZone[7]
    $localStoredTimestamp = _DateAdd("n", -$daylightSavingsBias, $localStoredTimestamp)
EndIf

; Get the current local time
$currentLocalTime = _NowCalc()

; Calculate the time difference in seconds
$timeDifferenceInSeconds = _DateDiff("s", $localStoredTimestamp, $currentLocalTime)

ConsoleWrite("Time Zone Bias: " & $timeZoneBias & @CRLF)
ConsoleWrite("Daylight Savings Setting: " & $daylightSavingsSetting & @CRLF)
ConsoleWrite("Stored UTC Timestamp: " & $utcStoredTimestamp & @CRLF)
ConsoleWrite("Converted Local Timestamp: " & $localStoredTimestamp & @CRLF)
ConsoleWrite("Current Local Time: " & $currentLocalTime & @CRLF)
ConsoleWrite("Time Difference (seconds): " & $timeDifferenceInSeconds & @CRLF)

It works and it returns me:

Time Zone Bias: -60
Daylight Savings Setting: 2
Stored UTC Timestamp: 2023-08-09 13:08:00
Converted Local Timestamp: 2023/08/09 15:08:00
Current Local Time: 2023/08/09 15:08:36
Time Difference (seconds): 36

I have two questions:

1. is is correct to subtract $timezoneBias and $daylightSavingsBias)

2. Does $tTimeZone[0] returns the current DST situation or just tells me that my country uses DST?

Thanks in advance

EDIT
I tried using it running on my VM running with PST where now it's 05:19:05 AM
Correct answer should be 8' (13:08:00 = 05:19:05 + 8hours time zone difference + 11 mins):

But this is the answer I get:

image.png.643060e2563b9196158db63d7c8ab39d.png

So not 8x60+8 = 488'' but -2935...

what's wrong?

 

 

Edited by marko001
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...