Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9

Thread: PHP Countdown

  1. #1
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP Countdown

    Hello I have a script that I found that grabs server time and then countdowns to the endtime (sept 20th in the script below) and when the endtime arrives it says Expired. Currently the way this script works is every 72 hours I have to go and add 3 days to the endtime to keep it running if I want it to countdown again. Question is: When it reaches Expired, How can I have the script wait 5 Mins and add 3 days to the endtime and in theroy, start another 72 hour countdown again! Without having to matain it anymore.

    Thanks ahead of time.
    - Jason


    <?php

    $endtime = "September 20th 2005";
    $enddate = strtotime($endtime);
    $off_d = date("d",$enddate);
    $off_m = date("m",$enddate);
    $off_y = date("y",$enddate);
    $off_th = date("H",$enddate);
    $off_tm = date("i",$enddate);
    $off_ts = date("s",$enddate);
    $curdate = time();

    $now_d = date("d",$curdate);
    $now_m = date("m",$curdate);
    $now_y = date("y",$curdate);
    $now_th = date("H",$curdate);
    $now_tm = date("i",$curdate);
    $now_ts = date("s",$curdate);
    ?>



    <script language="JavaScript1.2">
    now_d = <? echo $now_d; ?>;
    now_m = <? echo $now_m; ?>;
    now_y = <? echo $now_y; ?>;
    now_th = <? echo $now_th; ?>;
    now_tm = <? echo $now_tm; ?>;
    now_ts = <? echo $now_ts; ?>;
    off_y = <? echo $off_y; ?>;
    off_m = <? echo $off_m; ?>;
    off_d = <? echo $off_d; ?>;
    off_th = <? echo $off_th; ?>;
    off_tm = <? echo $off_tm; ?>;
    off_ts = <? echo $off_ts; ?>;
    dateNow = new Date(parseInt(now_y) + 2000,parseInt(now_m) - 1,now_d,now_th,now_tm,now_ts);
    dateFuture = new Date(parseInt(off_y) + 2000,parseInt(off_m) - 1,off_d,off_th,off_tm,off_ts);
    amount = dateFuture - dateNow;
    amount2 = amount;

    function update_time() {

    amount = amount2 - 1000;
    amount2 = amount;
    if(amount < 0){
    set_inner('countdown', 'Expired!');
    // window.location.reload();
    }else{
    hrs=0;
    mins=0;
    secs=0;
    amount = Math.floor(amount/1000);
    hrs=parseInt(Math.floor(amount/3600));
    amount=amount % 3600;
    mins=parseInt(Math.floor(amount/60));
    if (hrs < 10) hrs = '0'+hrs;
    if (mins < 10) mins = '0'+mins;
    amount=amount % 60;
    secs=parseInt(Math.floor(amount));
    if (secs < 10) secs = '0'+secs;
    set_inner('countdown', hrs+':'+mins+':'+secs);
    }
    setTimeout('update_time()', 1000);
    }
    function set_inner(name, value, add){
    var elem;
    if (document.getElementById) elem = document.getElementById(name);
    if (document.all) elem = document.all[name];
    if (elem && add) elem.innerHTML += value;
    if (elem && !add) elem.innerHTML = value;
    }
    </script>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Untitled Document</title>
    </head>
    <body topmargin="0" leftmargin="0" rightmargin="0" bottommargin="0" marginwidth="0" marginheight="0">


    <TABLE cellSpacing=0 cellPadding=0 width=97 border=0 style="color: #000000; font-weight: bold">
    <TBODY>



    <SCRIPT>update_time();</SCRIPT>



    <TR>
    <TD align=middle style="color: #333333; font-family: Verdana; font-size: 13pt; font-weight: bold" valign="top"><SPAN
    id=countdown><font size="4">TIME</font></SPAN></TD></TR></TBODY></TABLE></td>
    </body>
    </html>


    Here is a link to the working countown http://vip26.com/count.php
    Last edited by vip26; 09-18-2005 at 03:58 AM. Reason: add extra link to working script

  • #2
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post

    Ok, How do I change the:


    $endtime = "September 20th 2005"; on every 3rd day?

    by using

    $next_thrid_day = time() + (2 * 24 * 60 * 60);

    date('F jS, Y', $next_thrid_day)
    Last edited by vip26; 09-18-2005 at 06:50 AM.

  • #3
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Or is this gonna be easier running a root cronjob on the server every 3 days to edit the date in the php file and is that possibile?

  • #4
    New Coder
    Join Date
    Apr 2005
    Posts
    97
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, How do I change the:
    $endtime = "September 20th 2005"; on every 3rd day?

    by using

    $next_thrid_day = time() + (2 * 24 * 60 * 60);

    date('F jS, Y', $next_thrid_day)
    That really won't work, since next time the script is run, it wont remeber that you have set the the date, as the next third day...

    I.e if I did

    $x = 2;
    ...

    $x =5;

    next time I ran the script, it would start at x = 2 again.
    (I know, that is a bad explanation, it's 3AM here, so pardon it, please)

    My suggestion would to hold the date in an external txt file.

    I.e.

    PHP Code:
    $handle fopen("date.txt""r");
    $endtime fread($handle);
    $enddate strtotime($endtime);
    ... 
    Above might need tweaking, again, its 3AM.

    And then at the end of the script

    if amount < 0

    overwrite the txt file with the next third day..

    Understand? Or should I clarify once I sleep?

  • #5
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i'm not entirely sure it can be done completely automatic without a cron-job.
    you could store the enddate in a database or textfile, and have the script read it in from there. You should then have a cron that runs every day on midnight and that will then reset the date if the stored date is passed.
    on top of the enddate, you should have a begindate OR you should run a datediff between the current datetime and the enddate to see if the 5 minutes periode is already passed.

    frankly i'd be using a db and populate a table every 10 year or so that has 2 columns: begin_datetime and end_datetime.
    at the start of your script, you then just ru a select like
    SELECT enddatetimeFROM mytable WHERE Now() BETWEEN begin_datetime AND end_datetime
    If no record is returned, then you're in the 5 minutes timeout. Else you get one record with your enddate.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #6
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks!! , I just got the script to pull the enddate from date.txt, now I am gonna work on it some more. I am gonna explore on how to overwrite it on every 3rd day.

    - Jason
    Last edited by vip26; 09-18-2005 at 12:00 PM.

  • #7
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    that's just
    PHP Code:
    if (strtotime($endtime) < time()){
        
    $endtime $endtime + (3*24*60*60);
        
    // and write it to your file;

    but it's a lott more efficinet to use a db and prepopulate it ...
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #8
    New to the CF scene
    Join Date
    Sep 2005
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for everybodys help, I think I got it now. I managed to get a small script to overwrite date.txt with a (future date of 3 days away) anytime that php file gets accessed by anyone.

    I will use CRON to access that php file every 3 days at the right time Via Wget command, I just need to sync it and its finished.

    Thanks

  • #9
    New to the CF scene
    Join Date
    May 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    i'm trying to do the same thing..

    i found a countdown timer written in java script that i like.
    here is an example of it on my website www.photoduel.com, just click on one of the category tabs to see it.

    here is the code as of now.

    function CD_T(id,e){var n=new Date();
    CD_D(+n,id,e);
    setTimeout("CD_T('"+id+"',"+e+")",1100-n.getMilliseconds())};

    function CD_D(n,id,e){var ms=e-n;
    if(ms<=0) ms*=-1;var d=Math.floor(ms/864E5);
    ms-=d*864E5;var h=Math.floor(ms/36E5);
    ms-=h*36E5;
    var m=Math.floor(ms/6E4);
    ms-=m*6E4;
    var s=Math.floor(ms/1E3);
    CD_OBJS[id].innerHTML=d+" day"+(d==1?" ":"s ")+CD_ZP(h)+"h "+CD_ZP(m)+"m "+CD_ZP(s)+"s"};

    function CD_ZP(i){return(i<10?"0"+i:i)};

    function CD_Init(){var pref="countdown";
    var objH=1;
    if(document.getElementById||document.all){for(var i=1;objH;++i){var id=pref+i;objH=document.getElementById?document.getElementById(id):document.all[id];
    if(objH&&(typeof objH.innerHTML)!='undefined'){var s=objH.innerHTML;var dt=CD_Parse(s);
    if(!isNaN(dt)){CD_OBJS[id]=objH;
    CD_T(id,dt.valueOf());
    if(objH.style){objH.style.visibility="visible"}}
    else {objH.innerHTML=s+"<a href=\"http://andrewu.co.uk/clj/countdown/\" title=\"Countdown Error:Invalid date format used,check documentation (see link)\">*</a>"}}}}};

    function CD_Parse(strDate){var objReDte=/(\d{4})\-(\d{1,2})\-(\d{1,2})\s+(\d{1,2})\d{1,2})\d{0,2})\s+GMT([+\-])(\d{1,2}):?(\d{1,2})?/;
    if(strDate.match(objReDte)){var d=new Date(0);
    d.setUTCFullYear(+RegExp.$1,+RegExp.$2-1,+RegExp.$3);
    d.setUTCHours(+RegExp.$4,+RegExp.$5,+RegExp.$6);
    var tzs=(RegExp.$7=="-"?-1:1);
    var tzh=+RegExp.$8;
    var tzm=+RegExp.$9;
    if(tzh){d.setUTCHours(d.getUTCHours()-tzh*tzs)}if(tzm){d.setUTCMinutes(d.getUTCMinutes()-tzm*tzs)};
    return d}else {return NaN}};var CD_OBJS=new Object();
    if(window.attachEvent){window.attachEvent('onload',CD_Init)}
    else if(window.addEventListener){window.addEventListener("load",CD_Init,false)}
    else {window.onload=CD_Init}

    i would like to have this automatically run 5 days, then restart again running for 2 days, and repeat this over and over.

    right now, this script gets the date from the code on the webpage and displays the countdown till the date specified below:
    <span class="style24" id="countdown1"> 2006-05-10 00:00:00 GMT-04:00</span></td>

    i was thinking of editing the javascript to look to my database for the new date, but i dont know how to do this.

    any ideas?


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •