Topic: Php Date Time Calculation - Difference between 2 dates › WebmasterHub.net Forums

WebmasterHub.net Forums

Website & Application Design & Development, Programming, SEO, SharePoint Development & Administration.




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: Php Date Time Calculation - Difference between 2 dates
PostPosted: Wed Jan 06, 2010 2:47 am 
Contributer

Joined: Mon Aug 31, 2009 9:18 am
Posts: 32
How to calculate the difference between two dates (and times) using Php:

Calculate Date Difference - Option 1
-----------------------------------------------------
Code:
<?php   
    $dateDiff    = $date1 - $date2;   
    $fullDays    = floor($dateDiff/(60*60*24));   
    $fullHours   = floor(($dateDiff-($fullDays*60*60*24))/(60*60));   
    $fullMinutes = floor(($dateDiff-($fullDays*60*60*24)-($fullHours*60*60))/60);     
    echo "Differernce is $fullDays days, $fullHours hours and $fullMinutes minutes.";   
?>

"If you want to get the difference between to dates or time values, then you first need to get the values in the same format. The best way is to convert both dates into Unix timestamp format. In this case the date and time information is stored as an integer so we can calculate the difference easily."
See PHP date difference for details.


Calculate Date & Time Difference - Option 2
-----------------------------------------------------
"This function will find the difference between two given times. Does PHP already have a native function for that?"
Code:
//The difference between 2 given time

function dateDifference($day_1,$day_2) {
   $diff = strtotime($day_1) - strtotime($day_2);

   $sec   = $diff % 60;
   $diff  = intval($diff / 60);
   $min   = $diff % 60;
   $diff  = intval($diff / 60);
   $hours = $diff % 24;
   $days  = intval($diff / 24);

   return array($sec,$min,$hours,$days);
}

//Get the difference between days only:
$diff = strtotime($day_1) - strtotime($day_2) + 1; //Find the number of seconds
$day_difference = ceil($diff / (60*60*24)) ;  //Find how many days that is

Posted by Binny V A to txt.binnyva.com. See Find Difference Between 2 Dates in PHP for details.


Calculate Date Difference - Option 3
-----------------------------------------------------
Code:
function dateDiff($dformat, $endDate, $beginDate)
{
   $date_parts1=explode($dformat, $beginDate);
   $date_parts2=explode($dformat, $endDate);
   $start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);
   $end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);
   return $end_date - $start_date;
}

$date1="07/11/2003";
$date2="09/04/2004";

print "If we minus " . $date1 . " from " . $date2 . " we get " . dateDiff("/", $date2, $date1) . ".";

Posted by Amrit Hallan to developertutorials.com in the Php tutorials section. See Calculating Difference Between Two Dates Using PHP for details.


Calculate Date Difference - Option 4
-----------------------------------------------------
If you do not want or you cannot use Zend Framework or Pear package try this:
Code:
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)
    );
}

Posted by Irmantas to Stackoverflow.com (See PHP date calculation discussion for details.)


Calculate Date Difference - Option 5
-----------------------------------------------------
Using the Zend_Date Class:
Code:
// example printing difference in days
require('Zend/Date.php');

$date1 = new Zend_Date();
$date1->set(2, Zend_Date::MONTH);
$date1->set(27, Zend_Date::DAY);
$date1->set(2008, Zend_Date::YEAR);

$date2 = new Zend_Date();
$date2->set(3, Zend_Date::MONTH);
$date2->set(3, Zend_Date::DAY);
$date2->set(2008, Zend_Date::YEAR);

echo ($date2->getTimestamp() - $date1->getTimestamp()) / (3600*24);


Posted to StackOverflow.com by Ole J. Helgesen. See PHP Zend date calculation discussion for details.


Calculate Date Difference - Option 6
-----------------------------------------------------
Code:
function dateDiff($dformat, $endDate, $beginDate)
{   
   $date_parts1=explode($dformat, $beginDate);   
   $date_parts2=explode($dformat, $endDate);   
   $start_date=gregoriantojd($date_parts1[0], $date_parts1[1], $date_parts1[2]);         
   $end_date=gregoriantojd($date_parts2[0], $date_parts2[1], $date_parts2[2]);   
   return $end_date - $start_date;
}

$date1="07/11/2003";$date2="09/04/2004";
print "If we subtract " . $date1 . " from " . $date2 . " we get " . dateDiff("/", $date2, $date1) . ".";

Posted by pradeep to go4expert.com. See Calculating difference between two dates using PHP for details.


Calculate Date Difference - Option 7 (variation of Option 6 above)
-----------------------------------------------------
Code:
function daysDifference($endDate, $beginDate)
{

   //explode the date by "-" and storing to array
   $date_parts1=explode("-", $beginDate);
   $date_parts2=explode("-", $endDate);
   //gregoriantojd() Converts a Gregorian date to Julian Day Count
   $start_date=gregoriantojd($date_parts1[1], $date_parts1[2], $date_parts1[0]);
   $end_date=gregoriantojd($date_parts2[1], $date_parts2[2], $date_parts2[0]);
   return $end_date - $start_date;
}

//Usage:

<?php
   echo daysDifference('2008-03-12','2008-03-09');
?>

Posted by Roshan on roshanbh.com.np. See Finding difference of days between two dates in PHP for details.

-----------------------------------------------------
Related:
Php.net Date/Time Functions

Date Calculator
This is a simple script to calculate the difference between two dates and to express it in years, months and days. Feel free to use this script for whatever you want.

Date Calculator
"Date Calculator is a PHP script that allows you to calculate a date by adding or subtracting a certain number of days. This small php script was tested with apache, Linux, and Windows based systems."

PHP Class for Date Time calculations
"Performing data/time calculation is basic requirement in any programming project. No serious application can hide from it, so thankfully PHP itself comes with some very useful data/time functions."


Top
 Profile E-mail  
 
 Post subject: Re: Php Date Time Calculation - Difference between 2 dates
PostPosted: Thu Jan 07, 2010 2:49 am 
Contributer

Joined: Mon Aug 31, 2009 9:18 am
Posts: 32
More Php Date Calculation resources:

Php Class: Calculate Date Difference
Compute the time difference between two dates

Calculating date difference more precisely in PHP
"Following this post by Akash Mehta, I thought that a precise dateDiff() function might be helpful for many of you. The following function calculates the difference of two dates in “day” or “month” or “year” more precisely. This function is production ready and you can use it in any of your application which mainly works with these date difference. I have found it somewhere in web, just forgot the source. Thanks to the unknown author of this excellent function."


Top
 Profile E-mail  
 
 Post subject: Re: Php Date Time Calculation - Difference between 2 dates
PostPosted: Mon Jan 10, 2011 10:02 am 
Reader

Joined: Mon Jan 10, 2011 9:59 am
Posts: 4
Free and open source PHP Date Time Scripts. These Scripts mainly deal with the time and date in php also database and make the date & time output a easy way.
Hopefully can help you!

_________________
Free php script| PHP Template | PHP newsletter script


Top
 Profile E-mail  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
cron


SEO Tools & Resources Programming & Web Resources SharePoint Resources
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
DVGFX2 By: Matt