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

  • Home
  • SEARCH
  • 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 8951947
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:46:05+00:00 2026-06-15T13:46:05+00:00

Am trying to get the time difference between two days. But for certain date/time,

  • 0

Am trying to get the time difference between two days. But for certain date/time, I get wrong answers

Here is my code:

/****************************************
$start_date = new DateTime('23:58:40'); *These two still give 
$end_date = new DateTime('00:00:00');   *a wrong answer
*****************************************/

$start_date = new DateTime('23:58:40');
$end_date = new DateTime('00:11:36');

$dd = date_diff($end_date, $start_date);

//Giving a wrong answer: Hours = 23, Minutes = 47, Seconds = 4 
echo "Hours = $dd->h, Minutes = $dd->i, Seconds = $dd->s";
  • 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-15T13:46:05+00:00Added an answer on June 15, 2026 at 1:46 pm

    The awnser is correct. You provide two times. Without a date there is no way to know the last date is actually the next day. Just because you named the variable “end_date” doesnt mean PHP knows what you mean.

    Perhaps you should include the date aswell in your request like

    $start_date = new DateTime('2012-12-07 23:58:40');
    $end_date = new DateTime('2012-12-08 00:11:36');
    

    If you realy want to work with just times:

    function differenceInTimes($start, $end) {
        if (strtotime($start)>strtotime($end)) {
            //start date is later then end date
            //end date is next day
            $s = new DateTime('2000-01-01 '.$start);
            $e = new DateTime('2000-01-02 '.$end);
        } else {
            //start date is earlier then end date
            //same day
            $s = new DateTime('2000-01-01 '.$start);
            $e = new DateTime('2000-01-01 '.$end);
        }
    
        return date_diff($s, $e);
    }
    
    $start_date = '23:58:40';
    $end_date = '00:11:36';
    $dd = differenceInTimes($start_date, $end_date);
    echo "Hours = $dd->h, Minutes = $dd->i, Seconds = $dd->s";
    
    //Hours = 0, Minutes = 12, Seconds = 56 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Friends,I am trying to calculate the difference between two date&time entries that the user
I am trying to get the difference between time which is in the form
I am trying to calculate difference between two date strings both of which are
I'm trying to obtain the difference between two dates but i'm stuck at converting
I'm trying to get a difference between two dates in seconds. The logic would
I'm trying to get the exact time and date when users select a button,
I'm trying to get the unix time for date strings that are formatted like
I'm trying to get a feel for the difference in performance between integer multiplication
Possible Duplicate: how to calculate difference between two dates using java I'm trying something
I'm trying get LocalConnection to work between two swf's placed within two different IFRAMES.

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.