Page 1 of 2
Countdown between two dates
Posted: Sun Jul 30, 2006 4:54 pm
by NiGHTFiRE
Hey,
I've got two columns in my database that's dates. And I wanna see how long it's between them. Like Days, Hours, Minutes, Seconds. And I can't find any good scripts for that.
I get the date by doing like this:
Code: Select all
$plus_start = date('Y,m,d,H,i,s');
$plus_stop = date('Y,m,d,H,i,s', strtotime('+1 Month'));
Thanks.
Posted: Sun Jul 30, 2006 4:58 pm
by Ollie Saunders
You may find the solution is easier to achieve in SQL
Posted: Sun Jul 30, 2006 5:08 pm
by NiGHTFiRE
What should i search for on MySQLs documentation?
Thanks
Posted: Sun Jul 30, 2006 5:14 pm
by RobertGonzalez
Look for DATEDIFF() in the DATE/TIME Functions portion of their manual. Or search these boards for DATEDIFF with me as the author. I know that I have personally answered this type of question about five times in the past two weeks.

Posted: Sun Jul 30, 2006 5:33 pm
by NiGHTFiRE
Everah: Okey. Will do and between, sorry for asking it once again

Posted: Sun Jul 30, 2006 5:37 pm
by RobertGonzalez
It ok. All we ask is that you try searching and try coding before asking, if possible. Keep plugging along... none of us learned this overnight.
Posted: Sun Jul 30, 2006 5:40 pm
by NiGHTFiRE
Thanks guys. Found it in the doc. Btw Everah: When i searched for it I only got this post as a result

Posted: Sun Jul 30, 2006 11:59 pm
by RobertGonzalez
Maybe it is DATE_DIFF(), but when I did a forum search the other day for the other poster there were like 155 results returned. Maybe it was date difference. I forget. As long as you are closer now than before, then this community forum thingy is working...

Posted: Mon Jul 31, 2006 2:34 am
by NiGHTFiRE
Yes

Well I like it, but can it return hours as well?
Posted: Mon Jul 31, 2006 5:42 am
by ronverdonk
That is what the TIMEDIFF() function is for! Same MySql document chapter 12.5. Date and Time Functions.
Posted: Mon Jul 31, 2006 8:30 am
by panic!
What I normally do (this probably is a really bad way to do this!):
make a time stamp of the start and the end dates
find the difference - subtract start date from end date
then form a date based on the difference.
then subtract 1970 from the year returned.
bit of a hack and won't work with dates pre-epoch (1/1/1970)but I don't know any better way!
Posted: Mon Jul 31, 2006 9:47 pm
by RobertGonzalez
If you are pulling from the database, let the db server handle the math on it. It balances the load between servers and will ultimately speed up your script processing time.
Posted: Tue Aug 01, 2006 6:24 am
by NiGHTFiRE
Code: Select all
$start = $pengar_array['plus_start'];
$stop = $pengar_array['plus_stop'];
$plus_tid = "SELECT TIMEDIFF('{$start}', '{$stop}')";
$plus_tid_svar = mysql_query($plus_tid) or die(mysql_error());
And when i try to echo it: Resource id #5.
Thanks
Posted: Tue Aug 01, 2006 6:30 am
by JayBird
Code: Select all
$start = $pengar_array['plus_start'];
$stop = $pengar_array['plus_stop'];
$plus_tid = "SELECT TIMEDIFF('{$start}', '{$stop}') AS difference";
$plus_tid_svar = mysql_query($plus_tid) or die(mysql_error());
$row = mysql_fetch_array($plus_tid_svar);
echo $row['difference'];
Posted: Tue Aug 01, 2006 7:01 am
by NiGHTFiRE
Thanks pimptastic.
Just one question,
Well it stands on the same time all the time. Even if i shutdown my browser it still says 744 hours and all. It doesn't countdown :/