OxyScripts.com
Menu spacer Home Tutorials Articles Code Forums irc.freenode.net #oxyscripts
Main (PHP)
Home Forums PHP News PHP Tutorials Articles PHP Code Snippets Contact Us Sysadmin Resources Books Template Shop
3rd Party Streams
SlashDot PHPDeveloper.org PHP.Net
Resources
PHP Manual MySQL Manual Smarty Manual PEAR Manual PHP-GTK Manual Symfony Manual
Code Snippets
Authentication Database Graphics HTTP Miscellaneous Time/Date
Affiliates
Scripts TutorialMan TutorialGuide CodingForums.com PHP Scripts Cheap Web Hosting Affordable Web Hosting Dreamweaver Templates

Search This Site :     PHP Function Reference :
 

Countdown timer

By Chad (2002-04-05. 7192 views.)
Learn how to create a simple event timer to count down the days!

This tutorial will teach you how to create a simple event timer. An event timer will allow people to see the exact amount of days until something happens, such as a Birthday or webpage grand opening.


In the code below, I made a Birthday timer, but the code can be easily modified to time something else! Just follow the comments.

<?php

<?php
function time_until($event$day$month$year "unset"$min 0$hour 
0$sec 0) { 

    
/* The above prints the time until a given date. 
    This can calculate to any time of day. */ 

    /* If you didn't put a year above ($year = "unset"), assume it's the current year */ 

    
if ($year == "unset"
        
$year date("Y"); 

    
$left mktime($sec$min$hour$month$day$year) - mktime(); 

    
/* Lets assum that this is an annual event such as a Birthday and that
    it will happen again next year. This starts the counter over again after
    the event takes place. */ 
    
    
if ($left
        
$left mktime($sec$min$hour$month$day$year 1) - mktime(); 

    
/* If there is no time left on the counter (the day has come)
    Change the "My" if you want, but leave the $event for the event name
    This is set so it will say: "My Birthday" */
  
    
if ($left == 0
        return 
"My $event"

    
$time = array( 
        
"year" => 31449600
        
"week" => 604800
        
"day" => 86400
        
"hour" => 3600
        
"minute" => 60
        
"second" => 1); 

    
$ret_str "There "

    while(list(
$span$scale) = each($time)) { 
        
$chunk floor($left $scale); 
        
$left $left $scale

        if (
$chunk != 0): 

     
/* if there is more than one day left, have are, for 1 day have is */

            
$p1 = ($chunk == 1) ? "is" "are"

            if (
$chunk 1
                
$span "${span}s"

            
$sep = ($left == 0) ? " and " ", "

            
$ret_str .= ((!ereg("[0-9]""$ret_str")) ? $p1 $sep) . 
            
" $chunk $span"

        endif; 

    } 

    return 
$ret_str " until my ${event}."



     
/* As an example, how long until your Birthday comes? */ 
     /* ("Event", day of month, month); */
echo time_until("Birthday"129); 
?>

?>



Place this in a seperate php file and include it using something like php includes, or simply write it in your BODY tag where you want it to appear.


If you follow the comments correctly, you should come out with a wonderful way to time the days left for an event! This code can be heavily modified to do many things, so try it out!
 

 
   Print this page

Top Sponsor
Symantec\'s Norton SystemWorks 2006
Sponsors
CA
Sponsors
AdWords Dominator 125*125
Advertisting

Affiliates
VertexTemplates PHPFreaks CodeWalkers StarGeek DevScripts CGI & PHP Scripts PHP CMS

Shopping Rebates   Sell It 4 You   Flash Page Counters   Get Insured
GPS Tracking Service   Charity Donate Info   Web Site Hosting   VOIP Service

Privacy Policy | Links | Site Map | Advertising

All content on OxyScripts.com is (©)2002-2007

 
Powered by Adrastea - Version 1.0.0. Copyright © Rune Solutions, 2004-2005