savagenoob Posted January 16, 2009 Share Posted January 16, 2009 How do I take a database timestamp like (2009-01-15 20:07:06) and current time (20:19:15) strip the date and calculate the hours and minutes from the last timestamp to the current time. Link to comment https://forums.phpfreaks.com/topic/141032-solved-strip-date-from-database-timestamp-and-calculate-minutes/ Share on other sites More sharing options...
savagenoob Posted January 16, 2009 Author Share Posted January 16, 2009 Figured out how to strip the date... now the calculation... <?php $mytime = $myrow['Time']; $newtime= explode(' ', $mytime); echo $newtime[1]; ?> Link to comment https://forums.phpfreaks.com/topic/141032-solved-strip-date-from-database-timestamp-and-calculate-minutes/#findComment-738160 Share on other sites More sharing options...
savagenoob Posted January 16, 2009 Author Share Posted January 16, 2009 Wow, amazing how if I just do some research before posting I'd save myself some time typing . 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." Link to comment https://forums.phpfreaks.com/topic/141032-solved-strip-date-from-database-timestamp-and-calculate-minutes/#findComment-738163 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.