# I'm sick of making a Time calculator !

• (2 Pages)
• 1
• 2

## 22 Replies - 3009 Views - Last Post: 06 March 2009 - 04:02 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=90724&amp;s=b6080ae57fd7760115b6f23e371a4b95&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #16 samoi

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

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

Posted 05 March 2009 - 01:56 PM

CTphpnwb, 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 !

### #17 akozlik

Reputation: 91
• Posts: 798
• 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.

### #18 CTphpnwb

• D.I.C Lover

Reputation: 3782
• Posts: 13,708
• 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

### #19 samoi

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

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

Posted 05 March 2009 - 02:07 PM

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

### #20 akozlik

Reputation: 91
• Posts: 798
• 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.

### #21 samoi

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

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

Posted 05 March 2009 - 02:13 PM

CTphpnwb, 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

Thank you man, and I appreciate it ! really!

### #22 akozlik

Reputation: 91
• Posts: 798
• 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.

### #23 roudard

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

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

Posted 06 March 2009 - 04:02 AM

samoi, on 5 Mar, 2009 - 12:39 PM, said:

AlienWebguy, 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!

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

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