Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8081569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T16:46:16+00:00 2026-06-05T16:46:16+00:00

Time is confusing… How can one calculate the time elapsed from PM to AM

  • 0

Time is confusing… How can one calculate the time elapsed from PM to AM in php?

For example 22:00:00 (10pm) to 02:00:00 (02am) should give 04 hours elapsed. Instead my code returns -08 hours

function hours_elapsed() 
{           
    $timestart = strtotime("22:00:00");
    $timestop = strtotime("02:00:00"); 

    $time_diff = $timestop - $timestart; //time difference

    return gmdate("h", $total_hours);
}

It’s clear that the code calculates in 24 hour format so of course it returns -08 but how is it possible to get time elapsed without 24 hour constraint.. when it passes the midnight mark?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T16:46:17+00:00Added an answer on June 5, 2026 at 4:46 pm

    -8 hours is 4 hours. Just add 12 if the number is negative:

    $time_diff = $timestop - $timestart; //time difference
    
    if ($time_diff < 0) {
      $time_diff += 12;
    }
    

    This won’t help you, however, if your dates are more than one day apart. You need to specify the date as well (as in the day of the month) to tell PHP that those times are different.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A bit confusing this one but should make sense. Thanks to all your help
This is the second time I have got confusing results from the modulo operator,
have one time consuming step that flattens a bunch of files. basically i'd like
Many times i run time consuming PHP scripts that echo status updates like 'batch
Garbage Collection can become a time consuming process. In this regard, the GC tends
First time installing RoR and things are a bit confusing. I first installed Ruby
This is confusing as hell, here's the php I'm using: <?php echo date('H:i D
I have a really confusing problem. I have a PHP script that polls another
Sorry for the confusing title :P I'm sure I should know this but I
Maybe Im just confusing myself due to being pressed for time, but how would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.