Tandem Posted July 27, 2006 Share Posted July 27, 2006 *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. Quote Link to comment https://forums.phpfreaks.com/topic/15767-calculating-times/ Share on other sites More sharing options...
tomfmason Posted July 27, 2006 Share Posted July 27, 2006 check out the [url=http://us3.php.net/manual/en/function.time.php]time[/url] function. This should point you in the right directionor 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; 60secsecho 'Now: '. date('Y-m-d') ."\n";echo 'Next Week: '. date('Y-m-d', $nextWeek) ."\n";?> [/code] Quote Link to comment https://forums.phpfreaks.com/topic/15767-calculating-times/#findComment-64460 Share on other sites More sharing options...
Tandem Posted July 27, 2006 Author Share Posted July 27, 2006 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? Quote Link to comment https://forums.phpfreaks.com/topic/15767-calculating-times/#findComment-64473 Share on other sites More sharing options...
corbin Posted July 27, 2006 Share Posted July 27, 2006 $time3 = $time2 - date('Y-m-d H:i:s'); Quote Link to comment https://forums.phpfreaks.com/topic/15767-calculating-times/#findComment-64475 Share on other sites More sharing options...
Tandem Posted July 27, 2006 Author Share Posted July 27, 2006 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: 0Just a zero by itself!How come? Quote Link to comment https://forums.phpfreaks.com/topic/15767-calculating-times/#findComment-64476 Share on other sites More sharing options...
Tandem Posted July 27, 2006 Author Share Posted July 27, 2006 bump Quote Link to comment https://forums.phpfreaks.com/topic/15767-calculating-times/#findComment-64491 Share on other sites More sharing options...
corbin Posted July 27, 2006 Share Posted July 27, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/15767-calculating-times/#findComment-64492 Share on other sites More sharing options...
vinny_bc Posted July 27, 2006 Share Posted July 27, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/15767-calculating-times/#findComment-64508 Share on other sites More sharing options...
Tandem Posted July 27, 2006 Author Share Posted July 27, 2006 Is there anyway i can format it so i can use it so i can display for example 2m 30s rather than 150s? Quote Link to comment https://forums.phpfreaks.com/topic/15767-calculating-times/#findComment-64513 Share on other sites More sharing options...
vinny_bc Posted July 27, 2006 Share Posted July 27, 2006 Have a look at PHP's date() or strftime() functions. Quote Link to comment https://forums.phpfreaks.com/topic/15767-calculating-times/#findComment-64515 Share on other sites More sharing options...
sanfly Posted July 27, 2006 Share Posted July 27, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/15767-calculating-times/#findComment-64517 Share on other sites More sharing options...
Tandem Posted July 27, 2006 Author Share Posted July 27, 2006 I managed to format it with the date function. Thanks for all the help and replies! Quote Link to comment https://forums.phpfreaks.com/topic/15767-calculating-times/#findComment-64520 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.