Jump to content

[SOLVED] Strip Date from Database Timestamp and calculate Minutes


savagenoob

Recommended Posts

Wow, amazing how if I just do some research before posting I'd save myself some time typing  ;D. Here's my script for all who ever want to do the same.

<?php
$employee = $_SESSION['SESS_MEMBER_ID'];
$query = "SELECT clock, Time FROM timeclock WHERE employee = '$employee' ORDER BY ID DESC LIMIT 1";
$result = mysql_query($query);

while ($myrow = mysql_fetch_assoc($result))
{
	$time = date("H:i:s");
	echo "Last log: "; echo $time;
	echo "</br>";
	$mytime = $myrow['Time'];
	$newtime= explode(' ', $mytime);

	$finaltime = $newtime[1];

	$hours[0] = date("H.i",strtotime($finaltime));
$hours[1] = date("H.i",strtotime($time));

$hours[0] = ((int)$hours[0])*60 + ($hours[0]-((int)$hours[0]))*100;
$hours[1] = ((int)$hours[1])*60 + ($hours[1]-((int)$hours[1]))*100;
                         
if($hours[0] > $hours[1]) {
    $hours[1] += 24*60;
}
                             
$printtime = sprintf("%.2f",($hours[1] - $hours[0]) / 60);
$hstime = explode('.', $printtime);
echo "You've been logged "; echo $myrow['clock']; echo " for "; echo $hstime[0]; echo " hours and "; echo $hstime[1]; echo " minutes.";
echo "</br>";
?>

 

Will display "Last log: 21:05:52

You've been logged Out for 0 hours and 12 minutes."

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.