auto calculate time...

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

I have an array which I explode (using PHP) showing my work times, when I started, stopped and how long I worked for, it looks something like this:

"30/Aug/2003 15:09~30/Aug/2003 15:15~0:06 Hours",

it explodes by the "~" character.

is there a way using PHP that will automatically generate the "0:06 Hours" part? or in other words, calculate how long I worked.

note: the date and time has to be that format because its generated by editpad.

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Can't Editpad's timestamp format be customised? I'm sure it could... Confused

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

Options > Preferences > Editor (tab) > Date & time format

<?php
function time_from_editpad($str)
{
   
sscanf($str, \"%2d/%3s/%4d %2d:%2d\", &$mday, &$mon, &$year, &$hours, &$minutes);

    switch(strtolower(
$mon)) {
        case 'jan':
$mon = 0;   break;
        case 'fec':
$mon = 1;   break;
        case 'mar':
$mon = 2;   break;
        case 'apr':
$mon = 3;   break;
        case 'may':
$mon = 4;   break;
        case 'jun':
$mon = 5;   break;
        case 'jul':
$mon = 6;   break;
        case 'aug':
$mon = 7;   break;
        case 'sep':
$mon = 8;   break;
        case 'oct':
$mon = 9;   break;
        case 'nov':
$mon = 10;  break;
        case 'dec':
$mon = 11;  break;
    }

    return mktime(
$hours, $minutes, 0, $mon, $mday, $years);
}


echo \"<html><body><pre>\n\";

$txt = \"30/Aug/2003 15:09~30/Aug/2003 15:15~0:06 Hours\";

$tmp = explode('~', $txt);

$diff = time_from_editpad($tmp[1])-time_from_editpad($tmp[0]);

echo
$tmp[0].'~'.$tmp[1].'~'.str_pad(intval(($diff/60)/60), 2, '0', STR_PAD_LEFT).':'.str_pad((($diff/60)%60), 2, '0', STR_PAD_LEFT).' Hours';

echo \"</pre></body></html>\n\";


?>
Output: http://host.maxalbert.com/testing_center/date_exercise2.php
Source: http://host.maxalbert.com/testing_center/phps_files/date_exercise2.phps

Mark Hensler
If there is no answer on Google, then there is no question.

Renegade's picture

He has: 3,022 posts

Joined: Oct 2002

Great Smiling works like a charm. Thanks Mark Laughing out loud

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.