RESULT IN SECONDS - CONVERTING TO DAYS H M S

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
poeta_eletrico
Forum Commoner
Posts: 32
Joined: Mon Dec 22, 2003 7:33 am
Contact:

RESULT IN SECONDS - CONVERTING TO DAYS H M S

Post by poeta_eletrico »

feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]


Hi,


I know that s not a huge problem tat all and I really can play with but I was imagining another way to convert a query result given in SECONDS only to a common timestamp like: h: m: s ...
 
In Action:


I am retrieving data from mysql (the records comes in seconds) - I sum all the results returned, distinctly to every USER in seconds. But, sometimes I have more than one person and I would like to have the total amount, given in TIMESTAMP...any good ideas around?

I have this piece of code working:

Code: Select all

$num=($t_geral/86400);
	$days=intval($num);
	$num2=($num - $days)*24;
	$num3=($num2 - $days)*60;
	$mins = intval($num3);
	$num4=($num3-$mins)*60;
...any idea to have it in a different way?

Poeta_Eletrico


feyd | Please use

Code: Select all

and

Code: Select all

tags where appropriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Try using MYSQL's Date and Time functions.

http://dev.mysql.com/doc/refman/5.0/en/ ... tions.html
poeta_eletrico
Forum Commoner
Posts: 32
Joined: Mon Dec 22, 2003 7:33 am
Contact:

Post by poeta_eletrico »

hawleyjr wrote:Try using MYSQL's Date and Time functions.

http://dev.mysql.com/doc/refman/5.0/en/ ... tions.html
Hi,

I think that s not possible cos the time I have is in SECONDS not in TIMESTAMP to convert to ... I tried SEC_TO_TIME before but with no success.

Thanks.

Poeta_Eletrico
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

Are you asking for a function to convert seconds into the number of hours, minutes & seconds?
Last edited by hawleyjr on Tue Oct 25, 2005 2:27 pm, edited 1 time in total.
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

Use PHP's mktime() or date().
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

there's this too: viewtopic.php?t=29341
poeta_eletrico
Forum Commoner
Posts: 32
Joined: Mon Dec 22, 2003 7:33 am
Contact:

Post by poeta_eletrico »

foobar wrote:Use PHP's mktime() or date().
Hi,

But the format inserted, was not parsed the way that turns it possible...imagine I giving u something like

Code: Select all

$sec=234232;
Thank u for ur answer.

Poeta_Eletrico
poeta_eletrico
Forum Commoner
Posts: 32
Joined: Mon Dec 22, 2003 7:33 am
Contact:

Post by poeta_eletrico »

hawleyjr wrote:Are you asking for a function to convert seconds into the number of hours, minutes & seconds?
Hi,

It s not properly a FUNCTION but the best way and reasonable one...

Poeta_Eletrico
User avatar
n00b Saibot
DevNet Resident
Posts: 1452
Joined: Fri Dec 24, 2004 2:59 am
Location: Lucknow, UP, India
Contact:

Post by n00b Saibot »

have you seen the link feyd posted :?:
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

poeta_eletrico wrote:
foobar wrote:Use PHP's mktime() or date().
Hi,

But the format inserted, was not parsed the way that turns it possible...imagine I giving u something like

Code: Select all

$sec=234232;
Thank u for ur answer.

Poeta_Eletrico
I'm not really sure what you're trying to say here, but it seems as though you're having problems with the functions' syntax. Check the date() function for the format syntax.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

Ok, he is not looking for a DATE, he is looking to convert, for example, 23452340958024958 seconds into 15years, 5months, 3 weeks and 22days (or what ever it actually works out to be)

So the date() function is of no use.

Hence his mentioning of no timestamp, so stop suggesting he use the date() function!
poeta_eletrico
Forum Commoner
Posts: 32
Joined: Mon Dec 22, 2003 7:33 am
Contact:

Post by poeta_eletrico »

Jenk wrote:Ok, he is not looking for a DATE, he is looking to convert, for example, 23452340958024958 seconds into 15years, 5months, 3 weeks and 22days (or what ever it actually works out to be)

So the date() function is of no use.

Hence his mentioning of no timestamp, so stop suggesting he use the date() function!

Hi,


Sorry for my delay respond some posts here but at this moment I am on a vocation...although, Jenk is right...I know the time generated by my server is only in SECONDS...thats the way...

Thank all of you anyway...

Poeta_Eletrico
TJ
Forum Newbie
Posts: 20
Joined: Thu Nov 03, 2005 10:22 pm
Location: Nottingham, UK

Post by TJ »

This is concise:

Code: Select all

$t = 648478; // elapsed seconds
$s=($d=$t-604800*($weeks = floor($t/604800)))-($h=$d-86400*($days = floor($d/86400)))-($m=$h-3600*($hours = floor($h/3600)))-($seconds=$m-60*($minutes = floor($m/60)));
echo "elapsed $t seconds = $weeks weeks, $days days, $hours hours, $minutes minutes, $seconds seconds";
poeta_eletrico
Forum Commoner
Posts: 32
Joined: Mon Dec 22, 2003 7:33 am
Contact:

Post by poeta_eletrico »

TJ wrote:This is concise:

Code: Select all

$t = 648478; // elapsed seconds
$s=($d=$t-604800*($weeks = floor($t/604800)))-($h=$d-86400*($days = floor($d/86400)))-($m=$h-3600*($hours = floor($h/3600)))-($seconds=$m-60*($minutes = floor($m/60)));
echo "elapsed $t seconds = $weeks weeks, $days days, $hours hours, $minutes minutes, $seconds seconds";
TJ,


Perfect ! :-) Better than expected !
Thank you for your time consumed here writing this piece of code.

Poeta_Eletrico
Post Reply