21 Jun, 2009, flumpy wrote in the 2nd comment:

How would one count minutes, days, or seconds from a given time variable? Like if I wanted to do:

printf( "Account created %d days ago at %s.", timedays, ctime(&d->account->created));

to find the time in days and the created time was in milliseconds, you just do:

(timenow - timecreated) / 86400000

and round it.

[edit removed my dodgy maths]

21 Jun, 2009, Banner wrote in the 3rd comment:

if the created time was in milliseconds, you just do:

(timenow - timecreated) / 86400000

Meaning:

days = (current_time - acc_created_time) / 86400000

Yes?

EDIT: Well, makes more sense since you fixed it. :P

21 Jun, 2009, flumpy wrote in the 4th comment:

yea i hit reply too quickly :S

21 Jun, 2009, elanthis wrote in the 5th comment:

Basic math.

Take the difference in time in seconds, e.g. diff = time(NULL) - d->account->created (assuming that's a time_t variable), which will then have the total number of elapsed seconds. Then you just need to break it down into days, hours, minutes, and seconds.

The number of seconds is going to be diff % 60, and the total number of minutes is diff / 60.

The number of minutes is going to be the total number of minutes % 60, and the total number of hours is the total number of minutes / 60.

The number of hours is going to be the total number of hours % 24, and the number of days is the total number of hours / 60.

You can keep going to weeks or months if you want, of course.

So, in code off the top of my head, something like this:

You can then be a little smart with your printing code to elide the parts that are zero, e.g. don't say "0 hours ago" if the hours component comes out to 0.

(ninja'd!)

Take the difference in time in seconds, e.g. diff = time(NULL) - d->account->created (assuming that's a time_t variable), which will then have the total number of elapsed seconds. Then you just need to break it down into days, hours, minutes, and seconds.

The number of seconds is going to be diff % 60, and the total number of minutes is diff / 60.

The number of minutes is going to be the total number of minutes % 60, and the total number of hours is the total number of minutes / 60.

The number of hours is going to be the total number of hours % 24, and the number of days is the total number of hours / 60.

You can keep going to weeks or months if you want, of course.

So, in code off the top of my head, something like this:

time_t diff = time(NULL) - d->account->created;

unsigned seconds, minutes, hours, days;

seconds = diff % 60;

minutes = (diff / 60) % 60;

hours = (diff / 3600) % 24;

days = diff / 86400;

You can then be a little smart with your printing code to elide the parts that are zero, e.g. don't say "0 hours ago" if the hours component comes out to 0.

(ninja'd!)

21 Jun, 2009, flumpy wrote in the 6th comment:

Basic math.

Take the difference in time in seconds, e.g. diff = time(NULL) - d->account->created (assuming that's a time_t variable), which will then have the total number of elapsed seconds. Then you just need to break it down into days, hours, minutes, and seconds.

The number of seconds is going to be diff % 60, and the total number of minutes is diff / 60.

The number of minutes is going to be the total number of minutes % 60, and the total number of hours is the total number of minutes / 60.

The number of hours is going to be the total number of hours % 24, and the number of days is the total number of hours / 60.

You can keep going to weeks or months if you want, of course.

So, in code off the top of my head, something like this:

You can then be a little smart with your printing code to elide the parts that are zero, e.g. don't say "0 hours ago" if the hours component comes out to 0.

(ninja'd!)

Take the difference in time in seconds, e.g. diff = time(NULL) - d->account->created (assuming that's a time_t variable), which will then have the total number of elapsed seconds. Then you just need to break it down into days, hours, minutes, and seconds.

The number of seconds is going to be diff % 60, and the total number of minutes is diff / 60.

The number of minutes is going to be the total number of minutes % 60, and the total number of hours is the total number of minutes / 60.

The number of hours is going to be the total number of hours % 24, and the number of days is the total number of hours / 60.

You can keep going to weeks or months if you want, of course.

So, in code off the top of my head, something like this:

time_t diff = time(NULL) - d->account->created;

unsigned seconds, minutes, hours, days;

seconds = diff % 60;

minutes = (diff / 60) % 60;

hours = (diff / 3600) % 24;

days = diff / 86400;

You can then be a little smart with your printing code to elide the parts that are zero, e.g. don't say "0 hours ago" if the hours component comes out to 0.

(ninja'd!)

or you can do that..

21 Jun, 2009, Banner wrote in the 7th comment:

Well, that was easy enough. Thanks elanthis and flumpy. :)

Time to turn this into a function!

Time to turn this into a function!

Random Picks

0.0/7

Votes:0