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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 27
  1. #1
    Regular Coder
    Join Date
    Nov 2010
    Posts
    204
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Create a countdown clock whos base value is a UNIX timestamp...

    $contestend is in a UNIX format (ex. 1292907599).

    What I would like to know is this.

    ------- This referrel contest will end in '.$contestend.' -------

    How can I make the above bit read in a countdown clock type format so it might read something like this:

    ------- This referrel contest will end in ??Days, ??Hrs, ??Mins & ??Secs ------

    It doesnt have to be something that keeps counting if the user stays on the page for say 5 mins, just show the right figure on page load / refresh.

    Ty for your time !
    Last edited by MoreBloodWine; 12-21-2010 at 02:08 AM.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,700
    Thanks
    80
    Thanked 4,658 Times in 4,620 Posts
    Wrong forum. This is a JavaScript question. Well, okay, if you don't care about it being a *REAL* countdown you can do it in PHP. But why?
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Regular Coder
    Join Date
    Nov 2010
    Posts
    204
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Wrong forum. This is a JavaScript question. Well, okay, if you don't care about it being a *REAL* countdown you can do it in PHP. But why?
    By real countdown I assume you mean the live aspect of it like if I left the page open it would continue to count down. I just said to leave it out for ease of coding but if it's not that difficult then by all means I think it would be nice to have the live aspect of it.

    As to being in the wrong forum, I suppose I should call my bad on this one. I just assumed it could be done in PHP.

    In any case, if a mod would please move this to the Js board that would be really cool ;-)

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,700
    Thanks
    80
    Thanked 4,658 Times in 4,620 Posts
    To do this with JS, you'd simply pass both the *CURRENT* unixtime and the ending unixtime to the JS code. That is,
    Code:
    <script type="text/javascript">
    var curTime = <?php echo $currenttime; ?>;
    var endTime = <?php echo $endtime; ?>;
    
    ... and then let JS take over ...
    (Some would say you don't need to pass the current time, but if you don't, then you are hoping that the user's computer clock has the same current time as your server. So I think you are always better of passing both.)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Regular Coder
    Join Date
    Nov 2010
    Posts
    204
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    To do this with JS, you'd simply pass both the *CURRENT* unixtime and the ending unixtime to the JS code. That is,
    Code:
    <script type="text/javascript">
    var curTime = <?php echo $currenttime; ?>;
    var endTime = <?php echo $endtime; ?>;
    
    ... and then let JS take over ...
    (Some would say you don't need to pass the current time, but if you don't, then you are hoping that the user's computer clock has the same current time as your server. So I think you are always better of passing both.)
    Sounds good to me and good point on the clock thing... but how diccicult do you think it would be to write this up and approx how many lines of data do you think we'd be talking ?

    I suppose all that doesnt matter, for me anyway since I know absolutely nothing about Js unless I'm modifying existing code and even then I still have issues. With that said, I would need someone to write all the code for me if they wouldnt mind. With that said, to sort of make things easy it wouldnt have to be done right this minute but whenever said coder has the time to spare.

    Edit: As far as current server time does shouldnt that first echo be time();
    Code:
    <script type="text/javascript">
    var curTime = <?php echo time(); ?>;
    var endTime = <?php echo $contestend; ?>;
    
    ... and then let JS take over ...
    Last edited by MoreBloodWine; 12-20-2010 at 11:54 PM.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,700
    Thanks
    80
    Thanked 4,658 Times in 4,620 Posts
    As far as current server time does shouldnt that first echo be time();
    I dunno. I don't do PHP, remember? I could look it up, but I'll assume you are right.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    Regular Coder
    Join Date
    Nov 2010
    Posts
    204
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    I dunno. I don't do PHP, remember? I could look it up, but I'll assume you are right.
    Well, alot of times I open my mouth without thinking first but time() does output the server time in a UNIX format so AFAIK I am right on that much and $contestend outputs the contest end time in a unix format thx to this small bit.

    $contestend = strtotime('12/20/2010 11:59:59pm');

    Anyway, wether or not your able to help out with this is no big deal since you've done alot already in my other threads but sicne I havent said this yet or don't think I have... Thx for all the help you've given me these past few days ;-)

    Edit: Again, if a mod happens to stop by this thread could you please move it to the Js board, Thx !

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,700
    Thanks
    80
    Thanked 4,658 Times in 4,620 Posts
    Code:
    <html>
    <head>
    <script type="text/javascript">
    var curTime = Math.floor( (new Date()).getTime() / 1000 ); // replaced with PHP time(), presumably
    var endTime = 1293264000; // ditto
    
    function tick( )
    {
        var secs = endTime - curTime;
        var days = Math.floor( secs / 86400 ); // 86400 seconds in a day
        secs %= 86400;
        var hrs = Math.floor( secs/ 3600 );
        secs %= 3600;
        var mins = Math.floor( secs / 60 );
        secs %= 60;
        document.getElementById("ticker").innerHTML = 
            days + " days, " + hrs + " hours, " + mins + " minutes, " + secs + " seconds";
        ++curTime;
    }
    </script>
    </head>
    <body onload="setInterval(tick,1000); tick();">
    <h3>Time remaining is <span id="ticker"></span></h3>
    
    </body>
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #9
    Regular Coder
    Join Date
    Nov 2010
    Posts
    204
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Your my friend, if I may call you that... are a god !

    Looks good: http://www.eojmarket.com/referrals/ref.php

    Two things to note though, one of which you may not be able to help with since as previous said PHP isnt your thind.

    1. var endTime = <?php echo $contestend; ?>;

    That doesnt seem to work, $contestend is defined at the top of the file but using it in the above manner doesnt seem to work. Assuming OP doesnt have any ideas on this one does anyone else happen to know how to work the php $contestend variable in to the above Js line ?

    2. This one is directed at OP since it's his code... as a value reaches 0 is it possible to have it drop off so it might read something like below which covers all possible combinations where a figure reaches 0...

    1 Days, 5 Hrs, 28 Mins, 40 Secs

    5 Hrs, 28 Mins, 40 Secs

    28 Mins, 40 Secs

    40 Secs

    or even like this where a 0 might fall in the middle...

    1 Days, 28 Mins, 40 Secs

    1 Days, 5 Hrs, 40 Secs

    Also, to take this one step further if I may and you might understand this one but can we have the s drop off when times reach single digits so...

    1 Days becomes 1 Day 1 Hrs becomes 1 Hr etc...

    Code:
    <script type="text/javascript">
    var curTime = Math.floor( (new Date()).getTime() / 1000 ); // replaced with PHP time(), presumably
    var endTime = 1292907600; // ditto
    
    function tick( )
    {
        var secs = endTime - curTime;
        var days = Math.floor( secs / 86400 ); // 86400 seconds in a day
        secs %= 86400;
        var hrs = Math.floor( secs/ 3600 );
        secs %= 3600;
        var mins = Math.floor( secs / 60 );
        secs %= 60;
        document.getElementById("ticker").innerHTML = 
            days + " Days, " + hrs + " Hrs, " + mins + " Mins, " + secs + " Secs ";
        ++curTime;
    }
    </script>

  • #10
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,700
    Thanks
    80
    Thanked 4,658 Times in 4,620 Posts
    (1) Should work. echo the value in the <body> of the page, just to see what it is?

    (2) Fussy, fussy, fussy. *sigh*.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #11
    Regular Coder
    Join Date
    Nov 2010
    Posts
    204
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    (1) Should work. echo the value in the <body> of the page, just to see what it is?

    (2) Fussy, fussy, fussy. *sigh*.
    (1) Reads 1292907599
    See for yourself: http://www.eojmarket.com/referrals/ref.php

    This is how it's set up at the top of the page, not that I think it should matter since the echod value is a UNIX stamp.

    $contestend = strtotime('12/20/2010 11:59:59pm');

    (2) No big deal just thought I might ask, if nothing else I would atleast liek the s's to drop off as the values reach single digits.

    Ps; Your work is appreciated and to prove it I wil lpay for it, I just can't do anything before the 5th of Jan when my UC comes in.

  • #12
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,700
    Thanks
    80
    Thanked 4,658 Times in 4,620 Posts
    Code:
    <html>
    <head>
    <script type="text/javascript">
    var curTime = Math.floor( (new Date()).getTime() / 1000 ); // replaced with PHP time(), presumably
    var endTime = 1293264000; // ditto
    endTime = curTime + 75;  // JUST FOR DEMO PURPOSES
    var tock = null;
    
    function tick( )
    {
        var msg = "";
        var secs = endTime - curTime;
        ++curTime;
    
        if ( secs <= 0 )
        {
            clearInterval(tock);
            document.getElementById("remain").innerHTML = "This contest is over";
            return;
        }
    
        var show = false;
        var days = Math.floor( secs / 86400 ); // 86400 seconds in a day
        if ( days > 0 ) show = true;
        if ( show ) msg += days + ( days != 1 ? " days, " : " day, " );
    
        secs %= 86400;
        var hrs = Math.floor( secs/ 3600 );
        if ( hrs > 0 ) show = true;
        if ( show ) msg += hrs + ( hrs != 1 ? " hours, " : " hour, " );
    
        secs %= 3600;
        var mins = Math.floor( secs / 60 );
        if ( mins > 0 ) show = true;
        if ( show ) msg += mins + ( mins != 1 ? " minutes, " : " minute, " );
    
        secs %= 60;
        msg += secs + ( secs != 1 ? " seconds" : " second" );
    
        var t = document.getElementById("ticker");
        t.innerHTML = msg;
        if ( ! show ) t.style.color = "red";
    }
    </script>
    </head>
    <body onload="setInterval(tick,1000); tick();">
    <h3 id="remain">Time remaining is <span id="ticker"></span></h3>
    
    </body>
    </html>
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #13
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,700
    Thanks
    80
    Thanked 4,658 Times in 4,620 Posts
    Show the PHP code that creates that time value you dump to the top of the page.

    Show the PHP code you tried for specifying the curTime and endTime to JS.

    Actuallly, show your full PHP page code from the top down to where the JS tick() function starts.
    Last edited by Old Pedant; 12-21-2010 at 01:03 AM.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #14
    Regular Coder
    Join Date
    Nov 2010
    Posts
    204
    Thanks
    10
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    Show the PHP code that creates that time value you dump to the top of the page.

    Show the PHP code you tried for specifying the curTime and endTime to JS.
    The below code is everything from the file including the echo you suggested to display the value of $contestend

    I also noticed in the new code you posted you have a contest end message, thats not needed because I already have that defined using an if else statement which controls the top <tr>.

    Code:
    <?php
    
    	$contestbegin = strtotime('12/1/2010');
    	$contestend = strtotime('12/20/2010 11:59:59pm');
    
    	$prize = "New Copy of Uncharted 2: Among Thieves, GoTY Edition";
    	$prizepic = "u2goty.jpg";
    	$limit = 20;
    
    	$username="USER";
    	$password="PASS";
    	$database="DBASE";
    
    	mysql_connect('localhost',$username,$password);
    	@mysql_select_db($database) or die ("Unable to select database");
       
    	// Query Begin //
    	$sql = "SELECT u1.referred_by, u2.registered, COUNT(*) AS referral_count "
                 . " FROM ucard AS u1, ucard AS u2 "
                 . " WHERE IFNULL(u1.referred_by,'') != '' "
                 . " AND u1.referred_by = u2.psnid AND u2.registered BETWEEN $contestbegin AND $contestend "
                 . " GROUP BY u1.referred_by, u2.registered "
                 . " ORDER BY referral_count DESC LIMIT $limit";
    	$data = mysql_query($sql) or die(mysql_error() . '<br />Query: ' . $sql);
    
    	$request = mysql_query("SELECT COUNT(DISTINCT(`referred_by`)) FROM `ucard` WHERE `referred_by`!='' AND registered BETWEEN $contestbegin AND $contestend");
    	list($numreferrers) = mysql_fetch_row($request);
    	mysql_free_result($request);
    	// Query End //
    
    function addOrdinalSuffix($number) {
      if (in_array(($number % 100),array(11,12,13))){
        return $number.'th';
      }
      else {
        switch (($number % 10)) {
          case 1:
            return $number.'st';
            break;
          case 2:
            return $number.'nd';
            break;
          case 3:
            return $number.'rd';
            break;
          default:
            return $number.'th';
            break;
        }
      }
    }
    
    echo '
    <body style="background-color: #000000; color: #FFFFFF;" onload="setInterval(tick,1000); tick();">
    <center>
    ';
    
    echo $contestend;
    
    echo '
    <style type="text/css">
    table#places {
        border: solid 3px #9E853D;
    }
    table#places td {
        border: solid 1px #9E853D;
    }
    </style>
    
    <script type="text/javascript">
    var curTime = Math.floor( (new Date()).getTime() / 1000 ); // replaced with PHP time(), presumably
    var endTime = 1292907600; // ditto
    
    function tick( )
    {
        var secs = endTime - curTime;
        var days = Math.floor( secs / 86400 ); // 86400 seconds in a day
        secs %= 86400;
        var hrs = Math.floor( secs/ 3600 );
        secs %= 3600;
        var mins = Math.floor( secs / 60 );
        secs %= 60;
        document.getElementById("ticker").innerHTML = 
            days + " Days, " + hrs + " Hrs, " + mins + " Mins, " + secs + " Secs ";
        ++curTime;
    }
    </script>
    ';
    
    echo '
    <table id="places">
    ';
    
    	if(time() > $contestend){
    echo '
    		<tr align="center">
    		<td colspan="4">
    		------- Sorry, but this contest ended on '.date('D. F jS, Y', strftime($contestend)).' -------
    		</td>
    		</tr>
    ';
    	}else{
    echo '
    		<tr align="center">
    		<td colspan="4">
    		------- This referral contest will end in <span id="ticker"></span> -------
    		</td>
    		</tr>
    ';
    	}
    
    echo '
    	<tr align="center"> 
    	  <td colspan="3">Referral Contest Leaderboard<br />&nbsp;Number of Participants: '.$numreferrers.', Showing the top '.$limit.'&nbsp;</td>
    	  <td style="border: 1px solid; border-color: #9E853D;">1st Place Prize:<br />&nbsp;'.$prize.'&nbsp;</td>
    	</tr>
    ';
    
    echo '
    	<tr align="center">
    	  <td>
    	  &nbsp;Place&nbsp;
    	  </td>
    	  <td>
    	  &nbsp;Referrer ID&nbsp;
    	  </td>
    	  <td>
    	  &nbsp;Number of Referrals&nbsp;
    	  </td>
    	  <td rowspan="'.($limit + 1).'">
    	  <img width="407" height="515" src="http://www.eojmarket.com/referrals/prizes/'.$prizepic.'">
    	  </td>
    	</tr>
    ';
    
    $place = 0;
    
    for ($i = 0; $i < $limit; $i++) { 
    
    $place++;
    
        if ($i < mysql_num_rows($data)) { 
    	
            $row = mysql_fetch_array($data); 
    		$registered = $row["registered"];
    	
    echo '
    		<tr>
    		  <td>
    			'.addOrdinalSuffix($place).'
    		  </td>
    		  <td>
    			<a href="',"http://www.myu2sig.com/usergeneratedimages/", $row["referred_by"],".png", '" target="new_page">', $row["referred_by"], '</a> 
    			<font size="1">(';
    			if ($registered >= 1){
    			  echo date('n/j/Y', strftime($registered));
    			}else{
    			  echo 'Not Recorded';
    			}
    echo ')
    			</font>
    		  </td>
    		  <td>
    			'.$row["referral_count"].'
    		  </td>
    		</tr>
    ';
        } else { 
    echo '
    		<tr>
    		  <td>
    			'.addOrdinalSuffix($place).'
    		  </td>
    		  <td>
    		  &nbsp;
    		  </td>
    		  <td>
    		  &nbsp;
    		  </td>
    		</tr>
    ';
        } 
    }
    
    echo '
    </table>
    ';
    
    echo '
    </center>
    </body>
    ';
    
    ?>

  • #15
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,700
    Thanks
    80
    Thanked 4,658 Times in 4,620 Posts
    OH!!! You are one of those who uses echo in PHP to output huge chunks of HTML code!

    I think that's a mistake, but never mind. We'll leave it alone.

    so...
    Code:
    ...
    echo '
    <style type="text/css">
    table#places {
        border: solid 3px #9E853D;
    }
    table#places td {
        border: solid 1px #9E853D;
    }
    </style>
    
    <script type="text/javascript">
    var curTime = ' . time() . '; 
    var endTime = ' . $endtime . ';
    
    function tick( )
        ... continued ...
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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