News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

[Tip] Simple Countdown in header

Started by Marcus Forsberg, November 16, 2008, 06:20:36 AM

Previous topic - Next topic

Marcus Forsberg

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

Costa

LOL

pretty funny Nascar :)

Thank you for sharing with us.
Hugo "Costa" Fernandes - PT SMF
Todas as MP's a pedir ajuda são sujeitas a radioactividade, microondas, queimadas e atiradas borda fora.

"At least someone appreciates the fact that I am doing and not thinking..."
"Laziness is counter-revolutionary."

genieuk

Thank you  :D

Just what i wanted.

Regards,
Mathew

Dr.Stinglock

Would it be hard to make that work with a countdown script I found here ?

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";
}
?>


Those large signatures really look great!
Especially when it takes up ten times the size of the actual post!!

Marcus Forsberg

Actually that's a better script, rodregis :P

glennk

is it possible to count up ? I need one to count time gone since a certain event ??

Marcus Forsberg

This might work

Code (Find) Select

   $difference =($target-$today) ;


Code (Replace whit) Select

   $difference =($target+$today) ;

FlixyaFriend

samsung ue46c8000 [nofollow]


TheListener

Is there a demo of this working anywhere?

Nas this should be a mod.



Costa

Hugo "Costa" Fernandes - PT SMF
Todas as MP's a pedir ajuda são sujeitas a radioactividade, microondas, queimadas e atiradas borda fora.

"At least someone appreciates the fact that I am doing and not thinking..."
"Laziness is counter-revolutionary."

TheListener


qtime


raghhav

thanks for the good trick, can you tell me how to change the color and size of the font of output text

David12

Nice Code..! Thank you so much for your nice trick. I really Thankful to you. Keeping us post.

BigErn

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?

Masterd

#17
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!";

TheListener

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.

Advertisement: