Jump to content

Archived

This topic is now archived and is closed to further replies.

Tandem

Calculating times

Recommended Posts

*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.

Share this post


Link to post
Share on other sites
check out the [url=http://us3.php.net/manual/en/function.time.php]time[/url] function. This should point you in the right direction

or here is an example straight out of the [url=http://us3.php.net/manual/en/function.time.php]manual[/url]

[code=php:0]<?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";
?> [/code]

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Try

[code=php:0]strtotime($time5) - strtotime($time4)[/code]

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.

Share this post


Link to post
Share on other sites
Is there anyway i can format it so i can use it so i can display for example 2m 30s rather than 150s?

Share this post


Link to post
Share on other sites
Without seeing all your calculations, this is the best I can do....

Something along these lines should work

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

echo "$mins m $leftoverSec s";[/code]

Share this post


Link to post
Share on other sites
I managed to format it with the date function. Thanks for all the help and replies!

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.