www.webdeveloper.com
Results 1 to 5 of 5

Thread: Need Help Subtracting Dates in PHP

  1. #1
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357

    Question Need Help Subtracting Dates in PHP

    I have two dates: 2002-1-15 01:33:44 and 2006-7-13 02:36:11

    The first date is the date of the last login and the second date is the current time.

    I want to subtract these two times and spit out the time that has elapsed since the last login in these two formats: Y-m-d H:i:s AND M/j/Y g:i:s A

    Please believe me when I say I've looked all over for an answer to this but everything confused me even more. I'm tired of stressing over it so this is why I'm just asking.
    The better I get at programming, the more I appreciate arrays. Handy dandy things they are.

  2. #2
    Join Date
    May 2006
    Location
    the netherlands
    Posts
    454

    don't do it in php but in mysql

    since these are login times i guess you have them stored in a database. and in that case it's far more easy to do the math in the query that selects them. Since most PHPers seem to use mysql, i'm assuming you do as well:
    http://dev.mysql.com/doc/refman/5.0/...functions.html

    btw, this is one way to get the difference in PHP:
    PHP Code:
    $differenceInSeconds mktime() - strtotime("2002-1-15 01:33:44"); 
    Last edited by themarty; 07-13-2006 at 02:29 AM. Reason: additional info
    Free Scripts (PHP, Javascript, MySQL), homemade in the Excudo Devshed
    Create your own personal Startpage for free

  3. #3
    Join Date
    Jun 2006
    Location
    Under your bed
    Posts
    357

    Question

    It makes a number but how do I split it into seconds, miniutes, hours, days, months, and years? (e.g. It's been 1 year, 3 months, 2 days, 7 hours, 11 miniutes, and 33 seconds since so and so logged in)
    The better I get at programming, the more I appreciate arrays. Handy dandy things they are.

  4. #4
    Join Date
    Dec 2005
    Location
    India
    Posts
    490
    $hrs=floor($differenceInSeconds/3600);
    $mins=floor(($differenceInSeconds%3600)/60);
    $secs=($differenceInSeconds%3600)%60;

  5. #5
    Join Date
    May 2006
    Location
    the netherlands
    Posts
    454
    Reading the original post again, i don't think that is what's she's looking for. I only just realized myself.

    I don't know of any existing function that can do that, so i've written one myself. I tested it with a few dates and it seemed to work. It could contain a few bugs though:

    PHP Code:
    function dateDifference($start$end)
    {
         
    // converting the dates to seconds
        
    $startSeconds    strtotime($start);
        
    $endSeconds    strtotime($end);

         
    // if conversion was succesfull
        
    if ($startSeconds && $endSeconds)
        {
             
    // switching start and end date if start date is bigger
             // and converting them to 1 standard format for this function, so we know what we're dealing with
            
    if ($startSeconds $endSeconds)
            {
                
    $startDate date("Y-m-d H:i:s"$endSeconds);
                
    $endDate date("Y-m-d H:i:s"$startSeconds);
            }
            else
            {
                
    $startDate date("Y-m-d H:i:s"$startSeconds);
                
    $endDate date("Y-m-d H:i:s"$endSeconds);
            }

             
    // exploding everything into seperate variabels
            
    list($startDateDate$startDateTime) = explode(" "$startDate);
            list(
    $endDateDate$endDateTime) = explode(" "$endDate);

            list(
    $startYear$startMonth$startDay) = explode("-"$startDateDate);
            list(
    $endYear$endMonth$endDay) = explode("-"$endDateDate);

            list(
    $startHour$startMinute$startSecond) = explode(":"$startDateTime);
            list(
    $endHour$endMinute$endSecond) = explode(":"$endDateTime);

             
    // now we can start calculating
             // difference in seconds
            
    $secondDiff    $endSecond $startSecond;
            if (
    $startSecond $endSecond)
            {
                 
    // if the difference is negative, we add 60 seconds and increase the starting minute
                
    $secondDiff += 60;
                
    $startMinute++;
            }
            
    $minuteDiff    $endMinute $startMinute;
            if (
    $startMinute $endMinute)
            {
                
    $minuteDiff += 60;
                
    $startHour++;
            }
            
    $hourDiff    $endHour $startHour;
            if (
    $startHour $endHour)
            {
                
    $hourDiff += 24;
                
    $startDay++;
            }

             
    // days in starting month
            
    if ($endMonth $startMonth || $endYear $startYear)
            {
                if (
    $startDay $endDay)
                {
                     
    // amount of days this month has
                    
    $daysThisMonth date("t"$startDate);
                     
    // difference in days to the next month
                    
    $dayDiff    = ($daysThisMonth $startDay) + $endDay;
                     
    // compensating for the months
                    
    $startMonth++;
                }
                else
                    
    $dayDiff $endDay $startDay;
            }
            else
            {
                
    $dayDiff $endDay $startDay;
            }
            
    $monthDiff    $endMonth $startMonth;
            if (
    $startMonth $endMonth)
            {
                
    $monthDiff += 12;
                
    $startYear++;
            }
            
    $yearDiff    $endYear $startYear;


             
    // we know all the differences, so we're outputting that
            
    if ($yearDiff 0)
            {
                return 
    $yearDiff." year, ".$monthDiff." months, ".$dayDiff." days and ".$hourDiff." hours, ".$minuteDiff." minutes, ".$secondDiff." seconds";
            }
            elseif (
    $monthDiff 0)
            {
                return 
    $monthDiff." months, ".$dayDiff." days and ".$hourDiff." hours, ".$minuteDiff." minutes, ".$secondDiff." seconds";
            }
            elseif (
    $dayDiff 0)
            {
                return 
    $dayDiff." days and ".$hourDiff." hours, ".$minuteDiff." minutes, ".$secondDiff." seconds";
            }
            elseif (
    $hourDiff 0)
            {
                return 
    $hourDiff." hours, ".$minuteDiff." minutes, ".$secondDiff." seconds";
            }
            elseif (
    $minuteDiff 0)
            {
                return 
    $minuteDiff." minutes, ".$secondDiff." seconds";
            }
            elseif (
    $secondDiff 0)
            {
                return 
    $secondDiff." seconds";
            }
            else
            {
                return 
    "There is no difference!";
            }
        }
        else
        {
            return 
    False;
        }

    Free Scripts (PHP, Javascript, MySQL), homemade in the Excudo Devshed
    Create your own personal Startpage for free

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles