Part of the EllisLab Network
   
 
Date subtraction
Posted: 04 July 2009 10:08 AM   [ Ignore ]  
Summer Student
Avatar
Total Posts:  22
Joined  04-26-2009

Hi,
I need to perform some “simple” operations on dates, for example: getting the distance in days between two dates, or between now and a date in the past.

$previous_date = '2009-07-03';

// time() actually prints 2009-07-04

echo date('d', (time() - strtotime($previous_date));

This prints 02, but I’m not sure what it is. I mean, if it represents days, it should be 01.
Where am I wrong?

Thanks.

Profile
 
 
Posted: 04 July 2009 10:10 AM   [ Ignore ]   [ # 1 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  2051
Joined  06-04-2008

Why not use timespan() ?

Profile
 
 
Posted: 04 July 2009 10:35 AM   [ Ignore ]   [ # 2 ]  
Research Assistant
RankRankRank
Total Posts:  364
Joined  04-05-2007
$previous_date = '2009-07-03';
// time() actually prints 2009-07-04
$diff = time() - strtotime($previous_date);
echo
floor(($diff / (60 * 60 * 24)));

Not saying this is the best way to do it, just made your code example work.

Profile
 
 
Posted: 04 July 2009 12:08 PM   [ Ignore ]   [ # 3 ]  
Lab Technician
Avatar
RankRankRankRank
Total Posts:  2051
Joined  06-04-2008

In my views I make a call to a function in my helper:

echo pdb_rough_time_ago  (mysql_to_unix($msgdate)) . " ago";

$msgdate is in mysql format, of course.  One day I’ll write my helper function to be a bit more adept at recognising and coping with different date formats.

My helper contains:

/**
* shows pretty rough 'time ago' string
*
* @param string unix date stamp
* @return string
*/
function pdb_rough_time_ago ($date)  {
     
/// @TODO cope with non-unix date stamps
     /// @TODO cope with multiple parameters for showing arbitrary diff-between two dates
     /// @TODO get more sophisticated - if it's > 10 hours, don't show minutes .. that kind of thing
    
$difference = timespan ($date);

    
// If it's only minutes, or hours + minutes, don't mess with it,
    // otherwise just show two most significant components.
    
if ( substr_count($difference, ",") > 1)  {
        $date_array
= explode (", ", $difference);
        
$difference = $date_array[0] .", ". $date_array[1];
        
}

    
return $difference;
    
}

While this diverges slightly from the original question - it’s a ‘rough time’ example after all - it gives an idea of the benefits of using CI’s date helper (which is kind of the point isn’t it)?

Profile
 
 
Posted: 04 July 2009 12:56 PM   [ Ignore ]   [ # 4 ]  
Summer Student
Avatar
Total Posts:  22
Joined  04-26-2009
jedd - 04 July 2009 10:10 AM

Why not use timespan() ?

You’re right, I just didn’t see it :D

Thanks for the other solutions too.

Profile
 
 
   
 
 
Post Marker Legend
New Topic New posts Hot Topic Hot Topic with new posts New Poll New Poll Moved Topic Moved Topic Sticky Topic Sticky topic
Old Topic No new posts Hot Old Topic Hot Topic with no new posts Old Poll Old Poll Closed Topic Closed Topic Announcement Announcements
Theme
Change Theme
Visitor Statistics
The most visitors ever was 819, on March 11, 2010 11:15 AM
Total Registered Members: 119966 Total Logged-in Users: 53
Total Topics: 126092 Total Anonymous Users: 1
Total Replies: 663283 Total Guests: 523
Total Posts: 789375    
Members ( View Memberlist )