timestamp questions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dante2010
Forum Newbie
Posts: 1
Joined: Tue Feb 11, 2003 12:12 am

timestamp questions

Post 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??
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
aybra
Forum Commoner
Posts: 56
Joined: Sun Nov 24, 2002 12:52 am

Post by aybra »

Just wondering... how could you make this into a like a timesheet thing?
User avatar
daven
Forum Contributor
Posts: 332
Joined: Tue Dec 17, 2002 1:29 pm
Location: Gaithersburg, MD
Contact:

Post 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.
Post Reply