start date time end date time = total time

Discussion in 'PHP' started by selfAfflicted, Jan 3, 2008.

  1. #1
    I am looking for a script to take start date start time and end date end time and come out with a total hours spent. I guess you would call it a time tracker.

    start: 01/02/08 at 1300 (1PM)
    end: 01/03/08 at 0100 (1AM)

    total = ____ hours


    most of the problems I've been coming up with myself have been getting the total hours when a period of time extends over two or more days.

    thanks for your help!
     
    selfAfflicted, Jan 3, 2008 IP
  2. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #2
    kmap, Jan 3, 2008 IP
  3. selfAfflicted

    selfAfflicted Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for showing me the example website. I am trying to develop a script like that, the source code of the particular equation would be great to have, though I don't think anyone would have it.

    But in any case, if anyone could help me out with getting the actual code that would be able to do something like this I would appreciate it.

    Thanks
     
    selfAfflicted, Jan 3, 2008 IP
  4. kmap

    kmap Well-Known Member

    Messages:
    2,215
    Likes Received:
    29
    Best Answers:
    2
    Trophy Points:
    135
    #4
    hey the source is on that page just click View-> page source

    and you will see the javascript

    I advise not to copy that.Just rewrite it in your own way

    Regards

    Alex
     
    kmap, Jan 4, 2008 IP
  5. admins

    admins Peon

    Messages:
    74
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Has any code to you in PHP that can shows this Time related data?
     
    admins, Jan 4, 2008 IP
  6. selfAfflicted

    selfAfflicted Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I guess my question is the logic of capturing the amount of hours tracked.

    I don't really understand how I can find total hours when the time frame goes over a day, or from 11pm to 2am.

    I created a formula, but it isn't quite right.



    /*
    ----------------------------------------------------------------------------------
    a = start date
    b = stop date
    c = start time
    d = stop time

    (b - a) * 24 + d - c = x

    where x is the amount of hours in decimal form

    ----------------------------------------------------------------------------------
    */
     
    selfAfflicted, Jan 8, 2008 IP
  7. sharqi

    sharqi Guest

    Messages:
    105
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #7
    store all the times in the unix timestamp format

    references
    http://us2.php.net/strtotime
    http://us2.php.net/manual/en/function.time.php

    Basically store all the values as unix time stamps then its simple math to work out the time in hours/mins/secs or explore the many php functions for this kind of thing
     
    sharqi, Jan 9, 2008 IP
  8. Brewster

    Brewster Active Member

    Messages:
    489
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    60
    #8
    I've been using this code for timing in my projects:

    $time_start = 0;
    define( 'SHOW_SITE_STATS', true );
    
    if ( SHOW_SITE_STATS )
    {
    	// create our friendly timer functions :-)
    	$start_site_timer = create_function( '', 'global $time_start;$time_start = microtime(true);' );
    	$end_site_timer = create_function( '', 'global $time_start;return microtime(true) - $time_start;' );
    
    	// on your marks....
    	$start_site_timer();
    }
    PHP:
    If you want the number of hours, then you would need to divide the return value from the function $end_site_timer by the number of seconds in one hour.

    Hope this code helps.

    Brew
     
    Brewster, Jan 9, 2008 IP
  9. Barti1987

    Barti1987 Well-Known Member

    Messages:
    2,703
    Likes Received:
    115
    Best Answers:
    0
    Trophy Points:
    185
    #9
    This might help.

    Peace,
     
    Barti1987, Jan 9, 2008 IP
  10. selfAfflicted

    selfAfflicted Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    ok, so this is what I have

    <?php
    
    //Find the difference (MYSQL DATETIME/DATE Format)
    function find_difference($start_date,$end_date){
        list($date,$time) = explode(' ',$start_date);
        if($time == NULL){$time = '00:00:00';}
        $startdate = explode("-",$date);
        $starttime = explode(":",$time);
        
        list($date,$time) = explode(' ',$end_date);
        if($time == NULL){$time = '00:00:00';}
        $enddate = explode("-",$date);
        $endtime = explode(":",$time);
        
        $secons_dif = mktime($endtime[0],$endtime[1],$endtime[2],$enddate[1],$enddate[2],$enddate[0]) - 
    
    mktime($starttime[0],$starttime[1],$starttime[2],$startdate[1],$startdate[2],$startdate[0]);
    
        //Different can be returned in many formats
        //In Minutes: floor($secons_dif/60);
        //In Hours: floor($secons_dif/60/60);
        //In days: floor($secons_dif/60/60/24);
        //In weeks: floor($secons_dif/60/60/24/7;
        //In Months: floor($secons_dif/60/60/24/7/4);
        //In years: floor($secons_dif/365/60/24);
        
        //We will return it in hours
        $difference = floor($secons_dif/60/60);
        
        return $difference;
    }
    
    echo find_difference('2008-01-10 13:00:00','2008-01-11 12:00:00');
    ?> 
    Code (markup):



    now, I feel like an idiot because I can not get this part to work: I want the line that establishes two dates to compare:
    echo find_difference('2008-01-10 13:00:00','2008-01-11 12:00:00');
    Code (markup):
    To all be variables. I keep getting errors when I've tried to insert a variable into the line of code above.
    variables should be:


    (y=year, m=month, d=day)
    $y1 = 2008;
    $y2 = 2008;
    $m1 = 01;
    $m2 = 01;
    $d1 = 10;
    $d2 = 11;

    (h=hour, mi=minute, s=second)
    $h1 = 13;
    $h2 = 12;
    $mi1 = 00;
    $mi2 = 00;
    $s1 = 00;
    $s2 = 00;

    If anyone figures this out, please post the code for the variables, and for the find_difference line.

    Thank You
     
    selfAfflicted, Jan 10, 2008 IP
  11. selfAfflicted

    selfAfflicted Peon

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    found out how to do it.. here is the code.

    <?php
    
    //Find the difference (MYSQL DATETIME/DATE Format)
    function find_difference($start_date,$end_date){
        list($date,$time) = explode(" ",$start_date);
        if($time == NULL){$time = '00:00:00';}
        $startdate = explode("-",$date);
        $starttime = explode(":",$time);
        
        list($date,$time) = explode(" ",$end_date);
        if($time == NULL){$time = '00:00:00';}
        $enddate = explode("-",$date);
        $endtime = explode(":",$time);
        
    	//Get unix timestamp in seconds
        $secons_dif = mktime($endtime[0],$endtime[1],$endtime[2],$enddate[1],$enddate[2],$enddate[0]) - mktime($starttime[0],$starttime[1],$starttime[2],$startdate[1],$startdate[2],$startdate[0]);
    
        
        //We will return it in seconds, minutes, hours, days, weeks, months, years
    	//put the word floor before () to round to the nearest whole number.
    
    echo "The time between 2008-01-21 02:12:00 and 2010-01-21 02:12:00 is: <br />";
    $seconds = $secons_dif;
    $minutes = ($secons_dif/60);
    $hours = ($secons_dif/60/60);
    $days = ($secons_dif/60/60/24);
    $weeks = ($secons_dif/60/60/24/7);
    $months = ($secons_dif/60/60/24/7/4);
    $years = ($secons_dif/365/60/24/60);
    
    echo $seconds." seconds<br />".$minutes." minutes<br />".$hours." hours<br />".$days." days<br />".$weeks." weeks<br />".$months." months<br />".$years." years";
    	
    }
    $one = '2008-01-21 02:12:00';
    $two = '2008-01-22 02:12:00';
    echo find_difference("$one","$two");
    
    ?>
    Code (markup):
     
    selfAfflicted, Jan 11, 2008 IP
  12. jkapadia1983

    jkapadia1983 Peon

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    if you use time() function then following script can help you to find out difference...

    <?php
    $century = mktime(12, 0, 0, 1, 1, 2001);
    $today = time();
    $difference = $today - $century;
    echo 'This century started ';
    echo floor($difference / 84600);
    $difference -= 84600 * floor($difference / 84600);
    echo ' days, ';
    echo floor($difference / 3600);
    $difference -= 3600 * floor($difference / 3600);
    echo ' hours, ';
    echo floor($difference / 60);
    $difference -= 60 * floor($difference / 60);
    echo " minutes, and $difference seconds ago.";
    ?>
     
    jkapadia1983, Oct 1, 2009 IP