Countdown timer

Discuss PivotX 2.0.x extensions here, and view extensions that are available for download.

Countdown timer

Postby cjeukens » Mon Sep 16, 2013 6:46 pm

I would like to add a countdown timer to an Entry.

Q: Plenty of php code snippets to be found, but how do I insert a code like attached to an Entry?
Amy other way of accomplishing a similar result?

Code: Select all
<?php
     
    // Work with the numbers below:
    function countdown() {
   
        // The countdown date:
        $Year        = 2014;
        $Month        = 6;
        $Day        = 1;
        $Hours        = 9;
        $Minutes    = 0;
        $Seconds    = 0;
       
        // Countdown time:
        $Date = mktime($Hours, $Minutes, $Seconds, $Month, $Day, $Year, -1);
       
        // Date today:
        $Time = time();
     
        // Different between the two:
        $Different = $Date - $Time;
        ($Different < 0) ? $Different = 0 : false;
     
        // Calculate the remaining days, ours and minutes:
        $RemainingDays = floor($Different/60/60/24);
        $RemainingOurs = floor(($Different - $RemainingDays*60*60*24)/60/60);
        $RemainingMinutes = floor(($Different - $RemainingDays*60*60*24 - $RemainingOurs*60*60)/60);
        $RemainingSeconds = floor(($Different - $RemainingDays*60*60*24 - $RemainingOurs*60*60 - $RemainingMinutes*60));
     
    // onderstaande 3 regels zullen te zien zijn op uw scherm
    echo '
    <span class="launching">Lanceer datum binnen:</span>
    <div id="time">
        <div class="stamp">'.$RemainingDays.'<div class="cd-time">dagen</div></div>
        <div class="stamp">'.$RemainingOurs.'<div class="cd-time">uren</div></div>
        <div class="stamp">'.$RemainingMinutes.'<div class="cd-time">minuten</div></div>
        <div class="stamp">'.$RemainingSeconds.'<div class="cd-time">seconden</div></div>
    </div>'; }
   
    countdown();
   
?>
cjeukens
 
Posts: 9
Joined: Tue Jul 23, 2013 11:39 pm

Re: Countdown timer

Postby hansfn » Mon Sep 16, 2013 10:35 pm

This would be a typical, straight forward extension (that could take the countdown date as a parameter):

Code: Select all
<?php
// - Extension: Countdown snippet
// - Version: 0.1
// - Author: PivotX Team
// - Email: admin@pivotx.net
// - Site: http://www.pivotx.net
// - Description: A small countdown extension snippet.
// - Date: 2013-09-17
// - Identifier: countdown

// Register 'countdown' as a smarty tag.
$PIVOTX['template']->register_function('countdown', 'smarty_countdown');

/**
 * Outputs the countdown HTML based on the date.
 */
function smarty_countdown($params, &$smarty) {

    // The countdown date:
    $Year       = $params['year'];
    $Month      = $params['month'];
    $Day        = $params['day'];
    $Hours      = $params['hours'];
    $Minutes    = getDefault($params['minutes'], 0);
    $Seconds    = getDefault($params['seconds'], 0);

    // Countdown time:
    $date = mktime($Hours, $Minutes, $Seconds, $Month, $Day, $Year, -1);

    return _countdown_html($date);
}

function _countdown_html($Date) {
    // Date today:
    $Time = time();

    // Different between the two:
    $Different = $Date - $Time;
    ($Different < 0) ? $Different = 0 : false;

    // Calculate the remaining days, ours and minutes:
    $RemainingDays = floor($Different/60/60/24);
    $RemainingOurs = floor(($Different - $RemainingDays*60*60*24)/60/60);
    $RemainingMinutes = floor(($Different - $RemainingDays*60*60*24 - $RemainingOurs*60*60)/60);
    $RemainingSeconds = floor(($Different - $RemainingDays*60*60*24 - $RemainingOurs*60*60 - $RemainingMinutes*60));

    return '
    <span class="launching">Lanceer datum binnen:</span>
    <div id="time">
        <div class="stamp">'.$RemainingDays.'<div class="cd-time">dagen</div></div>
        <div class="stamp">'.$RemainingOurs.'<div class="cd-time">uren</div></div>
        <div class="stamp">'.$RemainingMinutes.'<div class="cd-time">minuten</div></div>
        <div class="stamp">'.$RemainingSeconds.'<div class="cd-time">seconden</div></div>
    </div>';
}

If you take the code above and save it in a file named extensions/snippet_countdown.php, enable the new Countdown extension in PivotX, you can use the following in your templates, entries and pages:

Code: Select all
[[ countdown year=2014 month=6 day=1 hours=9 ]]
hansfn
Developer
 
Posts: 3282
Joined: Sun Nov 25, 2007 7:48 pm
Location: Molde, Norway

Re: Countdown timer

Postby cjeukens » Mon Sep 30, 2013 10:00 am

Thanks, works great.
Have inserted the countdown in my header (http://opavontuurinnamibie.nl/)
cjeukens
 
Posts: 9
Joined: Tue Jul 23, 2013 11:39 pm


Return to 2.x Extensions

Who is online

Users browsing this forum: No registered users and 3 guests

cron