Countdown between two dates

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

Countdown between two dates

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

You may find the solution is easier to achieve in SQL
NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

Post by NiGHTFiRE »

What should i search for on MySQLs documentation?
Thanks
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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. :wink:
NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

Post by NiGHTFiRE »

Everah: Okey. Will do and between, sorry for asking it once again :)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

Post by NiGHTFiRE »

Thanks guys. Found it in the doc. Btw Everah: When i searched for it I only got this post as a result ;)
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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... :D
NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

Post by NiGHTFiRE »

Yes :) Well I like it, but can it return hours as well?
User avatar
ronverdonk
Forum Commoner
Posts: 34
Joined: Sat Jun 10, 2006 7:06 am
Location: Netherlands

Post by ronverdonk »

That is what the TIMEDIFF() function is for! Same MySql document chapter 12.5. Date and Time Functions.
User avatar
panic!
Forum Regular
Posts: 516
Joined: Mon Jul 31, 2006 7:59 am
Location: Brighton, UK

Post 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!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

Post 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
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post 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'];
NiGHTFiRE
Forum Contributor
Posts: 156
Joined: Sun May 14, 2006 10:36 am
Location: Sweden

Post 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 :/
Post Reply