Simple Machines Community Forum

Customizing SMF => Tips and Tricks => Topic started by: Marcus Forsberg on November 16, 2008, 06:20:36 AM

Title: [Tip] Simple Countdown in header
Post by: Marcus Forsberg on November 16, 2008, 06:20:36 AM
Hello!

What a beautiful day! A perfect day of making some PHP coding.
Let's place a countdown in our forum header!  :)




We'll start by finding ./Themes/default/index.template.php/.
If you do not use the SMF default theme, simply find the index.tempate.php of your current theme instead.

Then, we'll do one (Yes, only one) code edit in there:

Code (Find) Select
// display the time
echo '
<td class="titlebg2" height="32" align="right">
<span class="smalltext">' , $context['current_time'], '</span>



Code (Replace whit) Select
// display the time
echo '
<td class="titlebg2" height="32" align="right">
<span class="smalltext">' , $context['current_time'], ' ';
   $target = mktime(0, 0, 0, 12, 24, 2008) ;
   $today = time () ;
   $difference =($target-$today) ;
   $days =(int) ($difference/86400) ;
   print "(Only $days days to christmas!)";
   echo'</span>';


Note that if you use a custom theme, the code may look different. If you're not sure what to do, ask me.



When we have a look at that code, we See "0, 0, 0, 12, 24, 2008".
Now, what in the world is that?
Well, it's simply the date we're counting to.
12 is the month, 24 is the date and 2008 is the year.
I choosed to countdown the days to Christmas, change to what ever date you like. :)
We also see "   print "(Only $days days to Christmas!)";".
That's the message to display. $days are the number of days that are left, and basically what we're all here for.

And we're done.
Enjoy
Nascar
Title: Re: [Tip] Simple Countdown in header
Post by: Costa on November 16, 2008, 08:42:03 AM
LOL

pretty funny Nascar :)

Thank you for sharing with us.
Title: Re: [Tip] Simple Countdown in header
Post by: genieuk on November 16, 2008, 10:39:25 AM
Thank you  :D

Just what i wanted.

Regards,
Mathew
Title: Re: [Tip] Simple Countdown in header
Post by: Dr.Stinglock on November 18, 2008, 12:18:22 AM
Would it be hard to make that work with a countdown script I found here (http://elouai.com/countdown-clock.php) ?

I was just wanting to countdown hours/minutes as well.


<?php
// countdown function
// parameters: (year, month, day, hour, minute)
countdown(2010,1,1,0,0);

//--------------------------
// author: Louai Munajim
// website: www.elouai.com
//
// Note:
// Unix timestamp limitations 
// Date range is from 
// the year 1970 to 2038
//--------------------------
function countdown($year$month$day$hour$minute)
{
  
// make a unix timestamp for the given date
  
$the_countdown_date mktime($hour$minute0$month$day$year, -1);

  
// get current unix timestamp
  
$today time();

  
$difference $the_countdown_date $today;
  if (
$difference 0$difference 0;

  
$days_left floor($difference/60/60/24);
  
$hours_left floor(($difference $days_left*60*60*24)/60/60);
  
$minutes_left floor(($difference $days_left*60*60*24 $hours_left*60*60)/60);
  
  
// OUTPUT
  
echo "Today's date ".date("F j, Y, g:i a")."<br/>";
  echo 
"Countdown date ".date("F j, Y, g:i a",$the_countdown_date)."<br/>";
  echo 
"Countdown ".$days_left." days ".$hours_left." hours ".$minutes_left." minutes left";
}
?>


Title: Re: [Tip] Simple Countdown in header
Post by: Marcus Forsberg on November 18, 2008, 12:51:16 AM
Actually that's a better script, rodregis :P
Title: Re: [Tip] Simple Countdown in header
Post by: glennk on December 28, 2008, 03:31:36 PM
is it possible to count up ? I need one to count time gone since a certain event ??
Title: Re: [Tip] Simple Countdown in header
Post by: Marcus Forsberg on December 28, 2008, 03:34:25 PM
This might work

Code (Find) Select

   $difference =($target-$today) ;


Code (Replace whit) Select

   $difference =($target+$today) ;
Title: Re: [Tip] Simple Countdown in header
Post by: FlixyaFriend on January 19, 2009, 04:23:34 AM
Thank Dear Sir,Nas  :P
Title: Re: [Tip] Simple Countdown in header
Post by: enjoy1985 on January 25, 2009, 10:23:34 PM
Pretty nice tip
thanks you. :D
Title: Re: [Tip] Simple Countdown in header
Post by: TheListener on August 21, 2009, 02:14:53 PM
Is there a demo of this working anywhere?

Nas this should be a mod.

Title: Re: [Tip] Simple Countdown in header
Post by: jani01 on August 23, 2009, 03:27:44 PM
very nice tip.  Thanks from this end.

Title: Re: [Tip] Simple Countdown in header
Post by: Costa on August 23, 2009, 04:20:28 PM
Quote from: TheListener on August 21, 2009, 02:14:53 PM
Is there a demo of this working anywhere?

(http://i27.tinypic.com/2h3dki8.png)
Title: Re: [Tip] Simple Countdown in header
Post by: TheListener on August 23, 2009, 06:51:28 PM
Thanks bud
Title: Re: [Tip] Simple Countdown in header
Post by: qtime on November 18, 2009, 06:32:47 AM
nice code!
Title: Re: [Tip] Simple Countdown in header
Post by: raghhav on December 01, 2009, 10:03:47 AM
thanks for the good trick, can you tell me how to change the color and size of the font of output text
Title: Re: [Tip] Simple Countdown in header
Post by: David12 on December 30, 2009, 12:01:30 AM
Nice Code..! Thank you so much for your nice trick. I really Thankful to you. Keeping us post.
Title: Re: [Tip] Simple Countdown in header
Post by: BigErn on November 14, 2010, 09:27:18 PM
I'm trying to add a countdown timer to my header, but my code does not currently have the current time in it.

Would anyone be able to offer help on how to add this?
Title: Re: [Tip] Simple Countdown in header
Post by: Masterd on November 25, 2010, 12:56:49 PM
Quote from: cσσкιє мσηѕтєя on November 16, 2008, 06:20:36 AM
Code (Replace whit) Select
// display the time
echo '
<td class="titlebg2" height="32" align="right">
<span class="smalltext">' , $context['current_time'], ' ';
   $target = mktime(0, 0, 0, 12, 24, 2008) ;
   $today = time () ;
   $difference =($target-$today) ;
   $days =(int) ($difference/86400) ;
   print "(Only $days days to christmas!)";
   echo'</span>';


Isn't this hardcoded?

Also, echo is faster than print.

Index.template.php

echo '
<td class="titlebg2" height="32" align="right">
<span class="smalltext">' , $context['current_time'], ' ';
   $target = mktime(0, 0, 0, 12, 24, 2010) ;
   $today = time () ;
   $difference =($target-$today) ;
   $days =(int) ($difference/86400) ;
   echo $txt['christams_countdown'];


modifications.language.php

$txt['christams_countdown'] = "Only $days days to christmas!";
Title: Re: [Tip] Simple Countdown in header
Post by: TheListener on June 16, 2011, 02:14:29 PM
I know this topic is rather old (as am I) but it is worth the risk.

I am looking for a countdown timer where the seconds are actually moving (like the clock on MKAware).

I would also require the font size to be changeable.

The reason for this is due to a new radio station being launched in the local area.