PHP Date and Time

Discussion in 'PHP' started by ILiketohelp, Apr 26, 2009.

  1. #1
    Hey,
    I need something that checks the difference between two times. The format of the times is:

    Apr 26, 2009 1:42 PM

    Apr 26, 2009 5:44 PM

    It would subtract those and then output the difference in minutes. For example the times above would output 242 minutes. It also needs to check for the dates too.
     
    ILiketohelp, Apr 26, 2009 IP
  2. trukin

    trukin Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You could easily use strtotime. It lots of formats and returns the unix timestamp representation of it.

    
    $timeA = strtotime("Apr 26, 2009 1:42 PM");
    $timeB = strtotime("Apr 26, 2009 5:44 PM");
    
    $minutes = ceil(($timeB - $timeA) / 60);
    
    echo $minutes;
    
    PHP:
     
    trukin, Apr 26, 2009 IP
  3. ryandanielt

    ryandanielt Well-Known Member

    Messages:
    1,797
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    185
    #3
    The easiest way would be

     
    $time1 = 'Apr 26, 2009 1:42 PM';
    $time2 = 'Apr 26, 2009 5:44 PM';
    
    $total = $time1 - $time2;
    
    echo $total;
    
    PHP:
     
    ryandanielt, Apr 26, 2009 IP
  4. SmallPotatoes

    SmallPotatoes Peon

    Messages:
    1,321
    Likes Received:
    41
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I am concerned that you might be retarded.
     
    SmallPotatoes, Apr 26, 2009 IP
  5. ILiketohelp

    ILiketohelp Guest

    Messages:
    756
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I got it working with a big chunk of code. I'll give the first one a try but I know the second one won't work at all. Please don't post stupid stuff if you really have no clue what you're talking about.
     
    ILiketohelp, Apr 27, 2009 IP
  6. K1llswitch

    K1llswitch Member

    Messages:
    84
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    28
    #6
    :eek: Are you serious? :confused:
     
    K1llswitch, Apr 27, 2009 IP
  7. Grobbulus

    Grobbulus Peon

    Messages:
    557
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    haha. I agree with you. :)
     
    Grobbulus, Apr 27, 2009 IP
  8. ILiketohelp

    ILiketohelp Guest

    Messages:
    756
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Ya I mean really. Don't waste my time.
     
    ILiketohelp, Apr 29, 2009 IP
  9. zkvision

    zkvision Active Member

    Messages:
    59
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    #9
    function duration($start,$end) {
     $seconds = ($end - $start);
     
     
     $days = floor($seconds/60/60/24);
     $hours = $seconds/60/60%24;
     $mins = $seconds/60%60;
     $secs = $seconds%60;
     
     $duration='';
     if($days>0) $duration .= "$days days ";
     if($hours>0) $duration .= "$hours hours ";
     if($mins>0) $duration .= "$mins minutes ";
     if($secs>0) $duration .= "$secs seconds ";
     
     $duration = trim($duration);
     if($duration==null) $duration = '0 seconds';
     
     return $duration;
    }
    
    $duration =  duration($date1_stamp,$date2_stamp);
    
    
    PHP:
    define date1_stamp and date2_stamp, you can do it by converting the dates into unix stamp by strtotime function

    I hope this helps
     
    zkvision, Apr 30, 2009 IP
  10. ILiketohelp

    ILiketohelp Guest

    Messages:
    756
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I got it working thanks though...
     
    ILiketohelp, May 3, 2009 IP