I'm sick of making a Time calculator !

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

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 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:56 PM

View PostCTphpnwb, on 5 Mar, 2009 - 11:38 AM, said:

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";

?>



That just works exactly as I want !

But still get the code, and don't understand what some functions does !

I can try to know it, but I would like to get it from you guys like I got the idea of simplfying my code and make it simple ! :)


so the issues are:

the operator -= ??

and int($var) !

what they actually do during this conversion ?

sorry for bothering !
Was This Post Helpful? 0
  • +
  • -

#17 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 - 02:00 PM

-= takes the preceding variable and subtracts the variable afterwards and automatically assigns it.

$seconds -= $minutes * $minutes



That's shorthand for $seconds = $seconds - $minutes * $minutes;

Lets say seconds = 100 and minutes = 5.

$seconds -= $minutes * $minutes

would be equivalent to $seconds = 100 - 5*5

int($var) typecasts var to an integer. Lets say $var = 5.4. That would be a float. If you wanted to treat 5.4 as an integer 5, you would say (int)$var.
Was This Post Helpful? 0
  • +
  • -

#18 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2983
  • Posts: 10,313
  • Joined: 08-August 08

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

Posted 05 March 2009 - 02:01 PM

When you want to see how/what a function works, create a simple example:

<?php
$y=3.65;
$x = (int)$y;
echo $x;
?>

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

Was This Post Helpful? 0
  • +
  • -

#19 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 - 02:07 PM

Since you argue guys about giving me the answer !

I apologize for being pushy, but I really want to make my own code, and ALSO, get experince from you !

I learned through this using % is important in math problem ! and I forgot it because I learned PHP using books ! and hard to remember !

I also learned not to make the problem very hard !

and looking forward to learn the -= effects and int($var) :^:

Sorry guys, I made trouble here :(
Was This Post Helpful? 0
  • +
  • -

#20 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 - 02:10 PM

There's no trouble at all. What we're telling you is actually advice on how to solve problems. What phpnwb said is extremely important when learning to program in a language. He took an idea, built a small simple solution, and then took a look at the result.

% is the modulo. Just think of it as the remainder in division. If you took 10 / 3 you would get 3.33. If you think about basic arithmetic, that's 3 with a remainder of one. That one is what you would result from using modulous.

10 % 3 = 1

It's good for using when you want to do something on every nth iteration, like printing alternating colors for table rows or something.
Was This Post Helpful? 0
  • +
  • -

#21 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 - 02:13 PM

View PostCTphpnwb, on 5 Mar, 2009 - 01:01 PM, said:

When you want to see how/what a function works, create a simple example:

<?php
$y=3.65;
$x = (int)$y;
echo $x;
?>


Yeah, that's what I do usually. :)

I appreciate it.

but I just wanted if a new info will come to me quicker than trying :)

but making mistakes which let me learn more !

Thank you man, and I appreciate it ! really!
Was This Post Helpful? 0
  • +
  • -

#22 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 - 02:17 PM

There's no better way to learn than to do it yourself. It also keeps from learning from incorrect information. This was a pretty simple example, but if you start getting into more complex work it's easy to find people doing it the wrong way or suggesting the wrong thing. The best thing to do is always try on your own.
Was This Post Helpful? 0
  • +
  • -

#23 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 06 March 2009 - 04:02 AM

View Postsamoi, on 5 Mar, 2009 - 12:39 PM, said:

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:


What I mean is that the dates functions (prior to the introduction of the date object) get you all information you need about a date, you can subscribe, add do whatever you want on them, from a unix timestamp ... ie a number of seconds.

writing the date in a specific format is a fancy way of writing how many days, months, years, hours, seconds have elapsed between time 0 and the Unix timestamp you're interested in, that's how I see it as a time conversion :P

Maybe it's my mind is playing... you know ... twisted :blink:

I'm glad I was helpful anyway :D

Romain
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2