Page 1 of 1

timestamp questions

Posted: Tue Feb 11, 2003 12:12 am
by dante2010
I have a question on how to manipulate timestamps in PHP. Right now I have 2 seperate ones in the tools I'm writing. The first is recorded when a record is inserted into the system, and the second is a timestamp of the current date.

What I want to do is find the time difference between these 2 stamps and display it in a readable format. EX:

1 days 3 hours 34 mins

This would be displaying the time a record has been in the system.

Is there a way to do this??

Posted: Tue Feb 11, 2003 3:06 am
by twigletmac
Timestamps are just a count in seconds of the time since the UNIX epoch, so to get the difference between two you need to subtract one from the other and then you can use this bit of code which came from the PHP manual page on mktime():

Code: Select all

<?php

$now = mktime();
$before = mktime(0, 0, 0, 2, 10, 2003);

echo '$now: '.$now;
echo '<br />';
echo '$before: '.$before;

$seconds = ($now-$before);

$edays = floor($seconds / 86400); // get days 
$ehours = floor(($seconds % 86400) / (3600)); // get hours
$emins = floor((($seconds % 86400) % 3600) / 60); // get mins
$esecs = floor((($seconds % 86400) % 3600) % 60); // get secs

echo 'Total Seconds: '.$seconds;
echo '<br />';
echo 'Days: '.$edays.' - Hours: '.$ehours.' - Minutes: '.$emins.' - Seconds: '.$esecs;

?>
Obviously you'll need to cater the code to your own needs.

Mac

Posted: Tue Feb 11, 2003 11:45 am
by aybra
Just wondering... how could you make this into a like a timesheet thing?

Posted: Tue Feb 11, 2003 11:50 am
by daven
TimeSheet as in log employee hours?

Basically, have clockin/clockout buttons that make timestamps of the two times, and then get the difference between the values.

Granted, a good time sheet is more complex than that, but comparing two timestamps is all you should need.