I'm sick of making a Time calculator !

I want to convert seconds to hours & minutes & seconds !

  • (2 Pages)
  • +
  • 1
  • 2

22 Replies - 2227 Views - Last Post: 06 March 2009 - 04:02 AM Rate Topic: -----

#1 samoi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 12-February 09

I'm sick of making a Time calculator !

Post icon  Posted 05 March 2009 - 12:32 AM

Hello guys!

I was trying from last night to create something useful for myself instead of just changing codes and playing with it!
so I decided to make a script that can take an input of large number of seconds, and convert them to a time !

like if we take for example 90050 seconds, then It will be
1 day, 1:00:50

Second Example : 85000 seconds

will be
23:36:39

so I tried yesterday and today with no success ! :(

I could not find the way to solve this problem !!

here is my code!

<?php

error_reporting(E_ALL & ~ E_NOTICE);
function caltime($time) // $time will be in seconds !

{

	$second = round($time);
	// day sec
	$dSec = 86400;
	// hour sec
	$hSec = 3600;
	// minute sec
	$mSec = 60;


	$mins = 60;
	$hrs = 24;


	if ($second > $dSec) {

		$second = $second / $dSec;
		if ($second > $hrs) {
			$second = $second / $hrs;

		}
		
		else
		{
			if($second > $mins)
			{
				
			}
		}
		
		
	} else {
		if ($second > $hrs) {
			$second = $second / $hrs;

		}
	}

}



	if (isset($_POST['submit'])) {
		$sec = $_POST['sec'];

		echo caltime($sec);
	} else {

		echo '<form action="" method="post">
		how many? <input type="text" name="sec" />
		<br />
		<input type="submit" name="submit" value="Calculate" />
		</form>';

	}

?>



any suggestions??

This post has been edited by samoi: 05 March 2009 - 12:34 AM


Is This A Good Question/Topic? 0
  • +

Replies To: I'm sick of making a Time calculator !

#2 grimpirate  Icon User is offline

  • Pirate King
  • member icon

Reputation: 149
  • View blog
  • Posts: 714
  • Joined: 03-August 06

Re: I'm sick of making a Time calculator !

Posted 05 March 2009 - 01:08 AM

Take a look at the php date function.
Was This Post Helpful? 0
  • +
  • -

#3 samoi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 12-February 09

Re: I'm sick of making a Time calculator !

Posted 05 March 2009 - 01:22 AM

View Postgrimpirate, on 5 Mar, 2009 - 12:08 AM, said:

Take a look at the php date function.



Nothing helpful !

Have you gone through my thread ?

I want to CONVERT seconds to a time which means (large number of seconds to hours & mins & sec) like my examples above !



I DO NOT want the time formatting !

:)

BTW I have a nice exp. with php! not really a noob !
Was This Post Helpful? 0
  • +
  • -

#4 AlienWebguy  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 84
  • Joined: 04-March 09

Re: I'm sick of making a Time calculator !

Posted 05 March 2009 - 02:31 AM

First thing you need to do is add
return $second;
at the bottom of your function so you can at least see the output and debug.

--S--
Was This Post Helpful? 1
  • +
  • -

#5 roudard  Icon User is offline

  • D.I.C Head

Reputation: 17
  • View blog
  • Posts: 143
  • Joined: 26-September 05

Re: I'm sick of making a Time calculator !

Posted 05 March 2009 - 03:33 AM

View Postsamoi, on 5 Mar, 2009 - 12:22 AM, said:

I want to CONVERT seconds to a time which means (large number of seconds to hours & mins & sec) like my examples above !



I DO NOT want the time formatting !


date doesn't only do time formatting... well for me time formatting is a byproduct of conversion between units.
If you really want to rewrite what is already there, it's a simple math problem and I'd suggest the use of the % operator to find out how many days are in your number of seconds and what's left, then find the number of hours in what's left and what's left, then find the number of minutes in what's left and the number of seconds left.

Hope this helps

Romain
Was This Post Helpful? 1
  • +
  • -

#6 samoi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 12-February 09

Re: I'm sick of making a Time calculator !

Posted 05 March 2009 - 11:04 AM

View PostAlienWebguy, on 5 Mar, 2009 - 01:31 AM, said:

First thing you need to do is add
return $second;
at the bottom of your function so you can at least see the output and debug.

--S--



:)

You are always helpful :) :^:

I know this, and if you notice my function is not really completed ! but I just posted to show you the example of what I mean :)

Thank you anyway :)

View Postroudard, on 5 Mar, 2009 - 02:33 AM, said:

View Postsamoi, on 5 Mar, 2009 - 12:22 AM, said:

I want to CONVERT seconds to a time which means (large number of seconds to hours & mins & sec) like my examples above !



I DO NOT want the time formatting !


date doesn't only do time formatting... well for me time formatting is a byproduct of conversion between units.
If you really want to rewrite what is already there, it's a simple math problem and I'd suggest the use of the % operator to find out how many days are in your number of seconds and what's left, then find the number of hours in what's left and what's left, then find the number of minutes in what's left and the number of seconds left.

Hope this helps

Romain



Yes, it helps very well :)

I was confused for analyzing the problem :v:

but I have one more question, what other significant that date() function does besides formatting ? I would love to know :)
Was This Post Helpful? 0
  • +
  • -

#7 akozlik  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 90
  • View blog
  • Posts: 797
  • Joined: 25-February 08

Re: I'm sick of making a Time calculator !

Posted 05 March 2009 - 12:03 PM

RTFM
Was This Post Helpful? 0
  • +
  • -

#8 AlienWebguy  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 84
  • Joined: 04-March 09

Re: I'm sick of making a Time calculator !

Posted 05 March 2009 - 12:25 PM

Your problem has nothing to do with date(); since you're not trying to find a date, but rather a simple time duration conversion.

Try this for your function.. you can format the output however you want:

function caltime($time) // $time will be in seconds !

{

	$second = round($time);
	// day sec
	$dSec = 86400;
	// hour sec
	$hSec = 3600;
	// minute sec
	$mSec = 60;

	# BUFFER OUTPUT
	$return = '';

	# GRAB DAYS
	if ($second > $dSec) {

		$days = floor($second / $dSec);
		$second = $second % $dSec;
		$return.= $days.' days, ';
	}

	# GRAB HOURS
	if ($second > $hSec) {

		$hours = floor($second / $hSec);
		$second = $second % $hSec;
		$return.= $hours.' hours, ';
	}

	# GRAB MINUTES
	if($second > $mSec)
	{
		$mins = floor($second / $mSec);
		$second = $second % $mSec;
		$return.= $mins.' minutes ';

	}

	# RETURN ANSWER
	return $return;
}


Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3077
  • Posts: 10,788
  • Joined: 08-August 08

Re: I'm sick of making a Time calculator !

Posted 05 March 2009 - 12:38 PM

Heh, since we're giving the answers away:

You don't need if statements or functions for a basic math problem.
<?php
$seconds = 90050;
$day = 3600*24;
$hour = 3600;
$minute = 60;


$d = $seconds/$day;
$days = (int)$d;
$seconds -= $days * $day;
$h = ($seconds/$hour);
$hours = (int)$h;
$seconds -= $hours * $hour;
$m = $seconds/$minute;
$minutes = (int)$m;
$seconds -= $minutes * $minute;


echo $days." day(s) ".$hours." hours ".$minutes." minutes ".$seconds." seconds";

?>

This post has been edited by CTphpnwb: 05 March 2009 - 12:40 PM

Was This Post Helpful? 0
  • +
  • -

#10 AlienWebguy  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 84
  • Joined: 04-March 09

Re: I'm sick of making a Time calculator !

Posted 05 March 2009 - 12:54 PM

Sorry man didn't mean to break the rules. I just remember how frustrating it was when I was learning. I'll hold back next time.
Was This Post Helpful? 0
  • +
  • -

#11 akozlik  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 90
  • View blog
  • Posts: 797
  • Joined: 25-February 08

Re: I'm sick of making a Time calculator !

Posted 05 March 2009 - 01:18 PM

It's not necessarily a rule, we just try not to write other people's code for them. If you give a man a function, he'll program for a day. Teach a man to write his own functions, and he'll write his own programs for a lifetime.
Was This Post Helpful? 0
  • +
  • -

#12 samoi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 12-February 09

Re: I'm sick of making a Time calculator !

Posted 05 March 2009 - 01:22 PM

Hello Back, I fixed some, but can work with values of dayes and so, but cannot work with less than day or a day !

here is me code


<?php

error_reporting(E_ALL & ~ E_NOTICE);
function caltime($time) // $time will be in seconds !

{

	$second = round($time);
	// day sec
	$day = 86400;
	// hour sec
	$hour = 3600;
	// minute sec
	$minute = 60;


	$mins = 60;
	$hrs = 24;


	if ($second >= $day) {
		$days = floor($second / $day);
		$remain = $second % $day;

		if ($remain >= $hour) {
			$hours = floor($remain / $hour);
			$remain = $remain % $hour;

			if ($remain >= $minute) {
				$minutes = floor($remain / $minute);
				$remain = $remain % $minute;
			}
		}

	}


	if ($second >= $hour) {
		$hours = floor($second / $hour);
		$remain = $second % $hour;
		if ($remain >= $minute) {
			$minute = floor($remain / $minute);
			$remain = $remain % $minute;
		}
	}

	if ($second >= $minute) {
		$minutes = floor($second / $hour);
		$remain = $second % $minute;
	}


	return "$days day(s), " . str_pad($hours, 2, 0, STR_PAD_LEFT) . ':' . str_pad($minutes,
		2, 0, STR_PAD_LEFT) . ':' . str_pad($remain, 2, 0, STR_PAD_LEFT);
}


if (isset($_POST['submit'])) {
	$sec = $_POST['sec'];

	echo caltime($sec);
} else {

	echo '<form action="" method="post">
		how many? <input type="text" name="sec" />
		<br />
		<input type="submit" name="submit" value="Calculate" />
		</form>';

}

?>




I will reply to your replies guys :)
Was This Post Helpful? 0
  • +
  • -

#13 samoi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 12-February 09

Re: I'm sick of making a Time calculator !

Posted 05 March 2009 - 01:29 PM

View Postakozlik, on 5 Mar, 2009 - 11:03 AM, said:

RTFM


Be polite ! :v:

Help OR don't disturb !

I'm trying to learn and earn new experience ! with nice people around here like AlienWebguy :^: and the others !

Shame !
Was This Post Helpful? 0
  • +
  • -

#14 samoi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 39
  • Joined: 12-February 09

Re: I'm sick of making a Time calculator !

Posted 05 March 2009 - 01:39 PM

View PostAlienWebguy, on 5 Mar, 2009 - 11:25 AM, said:

Your problem has nothing to do with date(); since you're not trying to find a date, but rather a simple time duration conversion.


Maaaaaaaaaaaaan :^:

I love your patient on me :)

I don't know, but roudard said "date doesn't only do time formatting... well for me time formatting is a byproduct of conversion between units."

so, I thought it has something to do with converting time!

:blink:


View PostAlienWebguy, on 5 Mar, 2009 - 11:25 AM, said:

Try this for your function.. you can format the output however you want:

function caltime($time) // $time will be in seconds !

{

	$second = round($time);
	// day sec
	$dSec = 86400;
	// hour sec
	$hSec = 3600;
	// minute sec
	$mSec = 60;

	# BUFFER OUTPUT
	$return = '';

	# GRAB DAYS
	if ($second > $dSec) {

		$days = floor($second / $dSec);
		$second = $second % $dSec;
		$return.= $days.' days, ';
	}

	# GRAB HOURS
	if ($second > $hSec) {

		$hours = floor($second / $hSec);
		$second = $second % $hSec;
		$return.= $hours.' hours, ';
	}

	# GRAB MINUTES
	if($second > $mSec)
	{
		$mins = floor($second / $mSec);
		$second = $second % $mSec;
		$return.= $mins.' minutes ';

	}

	# RETURN ANSWER
	return $return;
}



That's very smart ! I tried it and it works, but I won't never ever think of it this way loool since I'm a beginner.

Please look at my code, I'm making it harder and longer ! but it was similar though! :P

I swear I didn't take your idea, but I took the advice of roundard! :)

Was This Post Helpful? 0
  • +
  • -

#15 akozlik  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 90
  • View blog
  • Posts: 797
  • Joined: 25-February 08

Re: I'm sick of making a Time calculator !

Posted 05 March 2009 - 01:44 PM

1. RTFM is from people not looking at the manual. If you want to know what the function does, read the manual and see! Don't ask someone to explain something that's already online.

2. I did a google search for "php convert seconds to minutes" and got this article. Looks like it does exactly what you need it to.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2