PDA

View Full Version : subtarct one timestamp from another


co0kz
05-12-2007, 03:31 PM
looking for a way to subtract one timestamp value from another and output the result as..

ie..

$remainingtime = $timestamp1 - $timestamp2

and then output $remainingtime as the below result

resulting in..

2 Days 10 Hours 33 Minutes 14 seconds. (example)

i know and understand how the timestamp value works but i can't figure out how i would split seconds into the above example.

I've searched both here and vb.com and done a wider search and can't seem to find how this could be done without altering core files whicvh i don't really want to do if i can help it.

Ideas on how this is acheivable would be greatly appreciated.

co0kz
05-16-2007, 12:45 PM
Anyone with any idea how this could be achieved?

UK Jimbo
05-16-2007, 01:14 PM
once you've subtracted them you have the difference in seconds and just have to do simple maths on it.

$seconds = $remainingtime % 60; // remainder when divided by 60
$minutes = floor($remainingtime / 60); // 60 seconds in a minute
$hours = floor($remainingtime / 3600); // 60 * 60 (secs in an hour)
$days = floor($remainingtime / 86400); // 60 * 60 * 24 (secs in a day)

nexialys
05-16-2007, 01:19 PM
actually it depend on what timestamp you want to evaluate ?!... posts or birthdays have different structures...

if it is posts, simple: X-Y=Z >> real time = vbdate(Z, true);

UK Jimbo
05-16-2007, 01:22 PM
If they're UNIX timestamps then the method I described would work.

If they're not then you could query any MySQL DATE/DATETIME field from the database using FROM_UNIXTIME(colname) to cast it into a UNIX timestamp.

eg SELECT id, name, FROM_UNIXTIMESTAMP(date_col) FROM table WHERE this='that'

co0kz
05-16-2007, 05:07 PM
hi there thanks for your help with this. After a little messing about with the code you provided (yes im dealing with unix timestamps) i managed to figure out where i was going wrong with this.

Thanks for you time and help. Much appreciated.