Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ajsmith_codes's avatar

Trouble with finding difference in times.

When one time is am and one is pm, this works fine. When both are am or both are pm, it's not working.

$timeIn is 7am (07:00) which I have to convert to DateTime and it becomes

DateTime @1647432000 {#2532
  date: 2022-03-16 07:00:00.0 America/Chicago (-05:00)
}

$timeOut is 11:30 am (11:30) and convert it becomes

DateTime @1647426600 {#2655
  date: 2022-03-16 11:30:00.0 +01:00
}

When I do $timeOut->diff($timeIn) I get this

DateInterval {#2656
  interval: - 01:30:00.0
  +"y": 0
  +"m": 0
  +"d": 0
  +"h": 1
  +"i": 30
  +"s": 0
  +"f": 0.0
  +"weekday": 0
  +"weekday_behavior": 0
  +"first_last_day_of": 0
  +"invert": 1
  +"days": 0
  +"special_type": 0
  +"special_amount": 0
  +"have_weekday_relative": 0
  +"have_special_relative": 0
}

Difference should be 4.5 hours, not 1.5.

0 likes
4 replies
sr57's avatar
sr57
Best Answer
Level 39

You have 2 different timezone (gmt -5 & -1) so 4h

Nakov's avatar

And you do notice the timezone difference, right?

tykus's avatar

The timezone offset changes

ajsmith_codes's avatar

I'm not certain why they are converting into different time zones. I use new DateTime() for both. I will start specifying the timezone and see what happens.

Please or to participate in this conversation.