Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Difference between 2 dates -- know something better?
Old 05-13-2008, 01:25 AM Difference between 2 dates -- know something better?
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
I needed to find the number of months between 2 dates. I don't know of a function in PHP, so I wrote this.

PHP Code:
<?php
define
('DATE_MONTH',1);
define('DATE_DAY',2);

function 
dateDifference($date_1$date_2$type=DATE_MONTH) {
  
$start_date strtotime($date_1);
  
$end_date strtotime($date_2);
  
$sign = ((int)($end_date-$start_date)/abs($end_date-$start_date));

  
$period_offset 0;
  if (
$sign 0) {
    switch (
$type) {
      case 
DATE_MONTH:
        while (
strtotime('+'.$period_offset.' months',$start_date) <= $end_date) {
          
$period_offset++;
        }
        break;
      case 
DATE_DAY:
        while (
strtotime('+'.$period_offset.' days',$start_date) <= $end_date) {
          
$period_offset++;
        }
        break;
    }
  } else {
    switch (
$type) {
      case 
DATE_MONTH:
        while (
strtotime('-'.$period_offset.' months',$start_date) <= $end_date) {
          
$period_offset++;
        }
        break;
      case 
DATE_DAY:
        while (
strtotime('-'.$period_offset.' days',$start_date) <= $end_date) {
          
$period_offset++;
        }
        break;
    }
  }
  return 
$period_offset;
}
echo 
dateDifference('5/1/2008','11/1/2008');
?>
Anyone know of a better way?
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE

Last edited by JeremyMiller; 05-13-2008 at 01:46 AM.. Reason: Fixed code bug
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
 
Register now for full access!
Old 05-13-2008, 03:20 AM Re: Difference between 2 dates -- know something better?
NullPointer's Avatar
Will Code for Food

Posts: 2,787
Name: Matt
Location: Irvine, CA
Trades: 0
I'm not in a position where I can test this myself but it makes since in my head:
PHP Code:
function numberOfMonths($date1$date2)
{
     
$diff abs(strtotime($date2) - strtotime($date1));
     
$month date('n'$diff);
     
$year date('o'$diff);
     
$deltaYears $year 1970

     
$deltaMonths $deltaYears 12 + ($month 1);
     return 
$deltaMonths;

I might be missing something but I think that will work, provided you don't use any dates prior to 1970. If it is the case that you are working with dates before 1970 just add the difference to each date (ie 1968 and 1969 become 1970 and 1971)
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 05-13-2008 at 03:31 AM.. Reason: Forgot to subtract 1 from the month
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 05-13-2008, 03:49 AM Re: Difference between 2 dates -- know something better?
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Thanks. That code doesn't actually work b/c of how $diff is calculated (it's meaning is distorted for the date function. I see your difference in logic, however, and recoded it like this:

PHP Code:
<?php
function numberOfMonths($date1$date2)
{
     
$diff abs(strtotime($date2) - strtotime($date1));
     
$month date('n'$diff);
     
$year date('o'$diff);
     
$deltaYears $year 1970;

     
$deltaMonths $deltaYears 12 + ($month 1);
     return 
$deltaMonths;
}
echo 
numberOfMonths('11/15/2008','2/1/2009').'<br /><br />';

function 
numMonths($date_1$date_2) {
  
$smallest_date min(strtotime($date_1),strtotime($date_2));
  
$smallest_year date('Y',$smallest_date);
  
$smallest_month date('m',$smallest_date);

  
$largest_date max(strtotime($date_1),strtotime($date_2));
  
$largest_year date('Y',$largest_date);
  
$largest_month date('m',$largest_date);

  
$years_between $largest_year $smallest_year 1;
  
$months_from_smallest 13 $smallest_month;

  if (
$largest_year == $smallest_year) {
    return 
$largest_month $smallest_month 1;
  } else {
    return 
$years_between*12 $months_from_smallest $largest_month;
  }
}
echo 
numMonths('11/15/2008','2/1/2009').'<br />';
echo 
numMonths('2/15/2008','11/1/2008').'<br />';
?>
The numMOnths function appears to work correctly, using your logic, and is more efficient than my original routine.

Thanks a lot!
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 05-13-2008, 08:38 PM Re: Difference between 2 dates -- know something better?
Novice Talker

Posts: 5
Name: absolut nod
Trades: 0
I need to find the number of days between 2 dates.
could you please help me??
abs0lut is offline
Reply With Quote
View Public Profile
 
Old 05-13-2008, 11:24 PM Re: Difference between 2 dates -- know something better?
Inet411's Avatar
Skilled Talker

Posts: 88
Name: programmer
Location: internet
Trades: 0
Here are some date functions I made that I use in many of my scripts:

PHP Code:
//convert unix to month day year
//returns array [0]->month [1]->day [2]->year
function unixToDate($unix) { 
    
$date date('m-d-y',$unix);
    
$date_array explode("-"$date);
    return 
$date_array;
}


//converts month day year to unix stamp
//sample usage:  $unix = dateToUnix(0, 0, 0, $_POST['month'], $_POST['day'], $_POST['year']);
function dateToUnix($month,$day,$year) {
    
$unix mktime(0,0,0,ltrim($month'0'),ltrim($day'0'),ltrim($year'0'));
    return 
$unix;
}

//returns the number of days difference between two dates
//dates must be sent to function in unix!
//returns number of days ie 1 2 ... 12 13 etc...
function dateDiff($earlier_date$later_date) { 
    
$diff_in_secs=$later_date $earlier_date;
    
$diff_in_days=$diff_in_secs 86400;
    return 
$diff_in_days;


I know that doesn't help find the number of months between two dates but maybe it would be useful somehow.... I see abs0lut could use the dateDiff function
__________________

Please login or register to view this content. Registration is FREE

Inet411 is offline
Reply With Quote
View Public Profile Visit Inet411's homepage!
 
Old 05-14-2008, 02:06 PM Re: Difference between 2 dates -- know something better?
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
OK. I sat down to redo the day calculation based off of Null's more intelligent implication and came up with this:
PHP Code:
<?php
function numDays($date_1,$date_2) {
  
$start_day strtotime(date('m/d/Y 00:00:00',strtotime($date_1)));
  
$end_day strtotime(date('m/d/Y 23:59:59',strtotime($date_2)));

  
$difference abs($end_day $start_day);
  return 
ceil($difference/(60*60*24));
}
echo 
numDays('1/1/08','1/3/08').'<br />';
echo 
numDays('2/28/08','3/1/08').'<br />';
echo 
numDays('1/1/08','12/31/08').'<br />';
echo 
numDays('1/1/07','12/31/08').'<br />';
?>
The idea, modified from the month one, is to start at the first second of the first day and end on the last second of the last day, so that seconds-based calculations are accurate.
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE

Last edited by JeremyMiller; 05-14-2008 at 02:06 PM.. Reason: Forgot the code tags!
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 05-15-2008, 01:31 AM Re: Difference between 2 dates -- know something better?
Novice Talker

Posts: 5
Name: absolut nod
Trades: 0
Quote:
Originally Posted by Inet411 View Post
Here are some date functions I made that I use in many of my scripts:

PHP Code:
//convert unix to month day year
//returns array [0]->month [1]->day [2]->year
function unixToDate($unix) { 
    
$date date('m-d-y',$unix);
    
$date_array explode("-"$date);
    return 
$date_array;
}


//converts month day year to unix stamp
//sample usage:  $unix = dateToUnix(0, 0, 0, $_POST['month'], $_POST['day'], $_POST['year']);
function dateToUnix($month,$day,$year) {
    
$unix mktime(0,0,0,ltrim($month'0'),ltrim($day'0'),ltrim($year'0'));
    return 
$unix;
}

//returns the number of days difference between two dates
//dates must be sent to function in unix!
//returns number of days ie 1 2 ... 12 13 etc...
function dateDiff($earlier_date$later_date) { 
    
$diff_in_secs=$later_date $earlier_date;
    
$diff_in_days=$diff_in_secs 86400;
    return 
$diff_in_days;

I know that doesn't help find the number of months between two dates but maybe it would be useful somehow.... I see abs0lut could use the dateDiff function
thank you very much
abs0lut is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Difference between 2 dates -- know something better?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 1.25547 seconds with 12 queries