Jump to content


Photo

Calculating times


  • Please log in to reply
11 replies to this topic

#1 Tandem

Tandem
  • Members
  • PipPipPip
  • Advanced Member
  • 251 posts

Posted 27 July 2006 - 02:19 AM

*please skip to my latest reply*

Ok, i'm tryin to calculate the difference between two times. One is a small distance into the future and is in the format(0000-00-00 00:00:00). Lets call this x.

The other is the current time, lets call this y.

I want to calculate how long it will be until y = x.

How should i go about doing this?

If you need more info just ask.

#2 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 27 July 2006 - 02:27 AM

check out the time function. This should point you in the right direction

or here is an example straight out of the manual

<?php
$nextWeek = time() + (7 * 24 * 60 * 60);
                   // 7 days; 24 hours; 60 mins; 60secs
echo 'Now:      '. date('Y-m-d') ."\n";
echo 'Next Week: '. date('Y-m-d', $nextWeek) ."\n";
?>

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#3 Tandem

Tandem
  • Members
  • PipPipPip
  • Advanced Member
  • 251 posts

Posted 27 July 2006 - 03:26 AM

How do i subtract the date from a variable date using single quotes?

Both are in the same format, i just don't know the syntax of single quotes and periods very well and am not sure how to use it in this situation.

e.g echo 'today '. $time2 .' - '. date('Y-m-d H:i:s') ."\n";

What should i change this to to make it actually calcualte the answer?

#4 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 27 July 2006 - 03:31 AM

$time3 = $time2 - date('Y-m-d H:i:s');
Why doesn't anyone ever say hi, hey, or whad up world?

#5 Tandem

Tandem
  • Members
  • PipPipPip
  • Advanced Member
  • 251 posts

Posted 27 July 2006 - 03:45 AM

I'm getting kinda confused here. Check this out.


$time5 = "2006-07-27 05:05:51";
$time4= date('Y-m-d H:i:s');
$time = $time5 - $time4;
echo "$time";

The output i'm getting is: 0
Just a zero by itself!
How come?

#6 Tandem

Tandem
  • Members
  • PipPipPip
  • Advanced Member
  • 251 posts

Posted 27 July 2006 - 04:21 AM

bump

#7 corbin

corbin
  • Staff Alumni
  • Advanced Member
  • 8,129 posts

Posted 27 July 2006 - 04:30 AM

lets say its currently 2006-7-56 11:30:50...

how can (2006-07-27 05:05:51) - (2006-7-56 11:30:50) be solved?  Thats your problem.
Why doesn't anyone ever say hi, hey, or whad up world?

#8 vinny_bc

vinny_bc
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 27 July 2006 - 04:54 AM

Try

strtotime($time5) - strtotime($time4)

This will give you the number of seconds difference between the two times.

Then it's up to you to calculate the number of mins, hours, days, or whatever you are looking for.



#9 Tandem

Tandem
  • Members
  • PipPipPip
  • Advanced Member
  • 251 posts

Posted 27 July 2006 - 05:19 AM

Is there anyway i can format it so i can use it so i can display for example 2m 30s rather than 150s?

#10 vinny_bc

vinny_bc
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 27 July 2006 - 05:24 AM

Have a look at PHP's date() or strftime() functions.

#11 sanfly

sanfly
  • Members
  • PipPipPip
  • Advanced Member
  • 344 posts
  • LocationNew Zealand

Posted 27 July 2006 - 05:26 AM

Without seeing all your calculations, this is the best I can do....

Something along these lines should work

$mins = $totalSec / 60;
$leftoverSec = $totalSec - ($mins * 60);

echo "$mins m $leftoverSec s";

If you're not part of the solution, you're part of the precipitate

#12 Tandem

Tandem
  • Members
  • PipPipPip
  • Advanced Member
  • 251 posts

Posted 27 July 2006 - 05:40 AM

I managed to format it with the date function. Thanks for all the help and replies!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users