How do calculate the difference between 2 time and date

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
8707
Forum Newbie
Posts: 11
Joined: Thu Jan 07, 2010 7:41 pm

How do calculate the difference between 2 time and date

Post by 8707 »

Hi all,

I've 2 strings that representing the start and end values of a date and time.For example, start date = 2010-01-30 14:50 and end date = 2010-02-02 12:30. How can i calculate the difference between this 2 string?

TQ
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: How do calculate the difference between 2 time and date

Post by AbraCadaver »

8707 wrote:Hi all,

I've 2 strings that representing the start and end values of a date and time.For example, start date = 2010-01-30 14:50 and end date = 2010-02-02 12:30. How can i calculate the difference between this 2 string?

TQ
Depends upon what you mean by the difference. The number of days, months and years between the two? The number of seconds between the two? Or what?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
pbs
Forum Contributor
Posts: 230
Joined: Fri Nov 07, 2008 5:31 am
Location: Nashik, India
Contact:

Re: How do calculate the difference between 2 time and date

Post by pbs »

You can use mktime() function to calculate difference between 2 dates.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How do calculate the difference between 2 time and date

Post by Christopher »

I would recommend using PHP's DateTime classes. See the manual.
(#10850)
klevis miho
Forum Contributor
Posts: 413
Joined: Wed Oct 29, 2008 2:59 pm
Location: Albania
Contact:

Re: How do calculate the difference between 2 time and date

Post by klevis miho »

Use strtotime() function:

$difference = strtotime($end_date) - strtotime($start_date); //will return the difference in seconds
8707
Forum Newbie
Posts: 11
Joined: Thu Jan 07, 2010 7:41 pm

Re: How do calculate the difference between 2 time and date

Post by 8707 »

Hi

Thank for the reply. In fact, I want to calculate the difference in (day, hours and minutes) or just (hours and minutes). I'm a newbie for PHP so at this moment i've no idea that how to calculate the difference between two date and time.

The date and time was save in text type which i capture it using $focus->column_fields['XXX'] = date('d-m-Y h:i A');
Therefore, anyone can kindly provide me a sample coding to calculate the difference between date and time and return the result in text.(hours and minutes or maybe include days)
TQ.

I found this from another website :

Code: Select all

function dateDifference($date1, $date2)
{
    $d1 = (is_string($date1) ? strtotime($date1) : $date1);
    $d2 = (is_string($date2) ? strtotime($date2) : $date2);
 
    $diff_secs = abs($d1 - $d2);
    $base_year = min(date("Y", $d1), date("Y", $d2));
 
    $diff = mktime(0, 0, $diff_secs, 1, 1, $base_year);
 
    return array
    (
        "years"                 => abs(substr(date('Ymd', $d1) - date('Ymd', $d2), 0, -4)),
        "months_total"  => (date("Y", $diff) - $base_year) * 12 + date("n", $diff) - 1,
        "months"                => date("n", $diff) - 1,
        "days_total"    => floor($diff_secs / (3600 * 24)),
        "days"                  => date("j", $diff) - 1,
        "hours_total"   => floor($diff_secs / 3600),
        "hours"                 => date("G", $diff),
        "minutes_total" => floor($diff_secs / 60),
        "minutes"               => (int) date("i", $diff),
        "seconds_total" => $diff_secs,
        "seconds"               => (int) date("s", $diff)
    );
}
Can i use this code directly or need to do some modification first?
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How do calculate the difference between 2 time and date

Post by Christopher »

(#10850)
8707
Forum Newbie
Posts: 11
Joined: Thu Jan 07, 2010 7:41 pm

Re: How do calculate the difference between 2 time and date

Post by 8707 »

Thank for the guidance from you all and i think my problem is about to solve. I'm using strtotime() to solve my problem and the date format is yyyy-mm-dd hh:mm or date('Y-m-d H:i'), if i change to dd-mm-yyyy, it will not get the correct result so i want to know that is it possible to change the date format to dd-mm-yyyy if using strtotime()?

Below is my solution :

Code: Select all

if ($focus->column_fields['xxx'] !="" && $focus->column_fields['yyy'] !="")
{
$start_date = $focus->column_fields['xxx'];
$end_date = $focus->column_fields['yyy'];
$seconds = strtotime($end_date) - strtotime($start_date);
$days = floor($seconds / 86400);
$seconds -= $days * 86400;
$hours = floor($seconds / 3600);
$seconds -= $hours * 3600;
$minutes = floor($seconds / 60);
$seconds -= $minutes * 60;
$focus->column_fields['cf_481'] ="{$days} days, {$hours} hours, {$minutes} minutes";
    
}
Post Reply