Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Prosty Countdown / Odliczanie jQuery + PHP
Forum PHP.pl > Forum > PHP
webfreak
Witam,

Mam taki oto kod ktory ciagnie mi z bazy Czas otwarca i zamkniecia w H:i chcialbym teraz do tego podpiac odliczanie w jquery ale tylko godziny i minuty ewentualnie sekund. Drugie pytanie to jak widac ostatni if chcialem zeby sprawdzal czy rzad OpeningTimes zawiera slowo Closed jesli tak to nie pokaze calkien inna wiadomosc niz te wszystko powyzej. Dosc jasno napisane ? Tylko prosze bez dennych odpowiedzi z typu " wyrafinowany techniczny jezyk " prosto jak krowie na rowie bez zadnych "antykwanta syntezowa lapidarnie stosowana" oraz odpowiedzi typu " Tu jest licznik tylko edytuj i bedzie dzialac " nie znam jquery i kazda debilna odpowiedz bedzie od razu ignorowana


  1. <?
  2.  
  3. include('config.php');
  4.  
  5. $day=date("l");
  6.  
  7. $tbl_name="OpeningTimes";
  8.  
  9. $sql="SELECT * FROM $tbl_name WHERE DayName='$day'";
  10. $result=mysql_query($sql);
  11.  
  12. ?>
  13. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  14. <html xmlns="http://www.w3.org/1999/xhtml">
  15. <head>
  16. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
  17. <title>Untitled Document</title>
  18.  
  19. </head>
  20.  
  21. <body>
  22.  
  23. <? date_default_timezone_set('Greenwich'); ?>
  24.  
  25. <? $rows=mysql_fetch_array($result) ?>
  26.  
  27. <?
  28.  
  29. $openingtime = strtotime($rows['OpeningTime']); //Opening time
  30. $closingtime = strtotime($rows['ClosingTime']); //Closing time
  31. $timenow = strtotime('now'); //Current Server Time
  32. $twentyfourhours = "24:00"; //Hours in a day
  33.  
  34. //echo date('G:i', $openingtime);
  35. //echo date('G:i', $closingtime);
  36. //echo date('H:i', $timenow);
  37. //echo $twentyfourhours;
  38.  
  39.  
  40. $openingin = $twentyfourhours - $timenow + $openingtime; //21:00 - current time
  41. $closingdown = $closingtime - $timenow; //
  42.  
  43.  
  44. if (date('H:i', $timenow) > date('H:i', $closingtime))
  45. {
  46. echo "The shop will be open again in ".date('H:i', $openingin)." hours";
  47. }
  48. elseif (date('H:i', $timenow) < date('H:i', $openingtime))
  49. {
  50. echo "The shop will open again in ".date('H:i', $openingin)." hours";
  51. }
  52. elseif (date('H:i', $timenow) < date('H:i', $closingtime))
  53. {
  54. echo "The shop will close in ".date('H:i', $closingdown)." hours";
  55. }
  56. elseif (!isset($rows["OpeningTime"])|| $rows['OpeningTime']==NULL || $rows['OpeningTime']==0)
  57. {
  58. echo "sorry but we are Closed today";
  59. }
  60.  
  61.  
  62. ?>
  63.  
  64. <? mysql_close(); ?>
  65. </body>
  66. </html>
Niktoś
Raczej będzie Ci potrzebny JS /JQ gdyż takie odliczanie będzie musiało odbywać się po stronie klienta lub asynchronicznie AJAX. Żeby nie za bardzo obciążać serwera można by z pomocą Ajaxa i formatu JSON odwoływać się bezpośrednio do funkcji w której mogą być dokonywane obliczenia czasu.
Tutaj taki przykładzik:
http://stackoverflow.com/questions/9904272...ction-from-json
webfreak
Bo znalazłem mase skryptów które odliczają ale po calej dacie e.g 208/12/05 12:35 a ja potrzebuje tylko odliczac czas e.g czas koncowy to 13:35 wszystko mam juz obliczone oblicza mi poprawnie teraz jak do tego podlaczyc liczniczek zeby tylko czas odliczalo bez potrzeby daty. Jesli odwiedzajacy ten temat nie moze nic poradzic na licznik niech ktos mi powie dlaczego moj ostatni elseif nie dziala smile.gif w sensie ostatni if sprawdza ( chyba ) czy rzad OpeningTimes zawiera NUll lub 0 i powinna wyswietlic sie wiadomosc jesli ten rzad jest pusty ale nic z tego

Ok znalazlem skrypt z ktorym prawdopodobnie bym sobie poradzil mianowicie zamienic ale teraz mam takie cosik


RESOLVED: Udalo sie przeniesc dwukropek
  1. if (li % 3 == 0){
  2. newComma = doc.createElement("ul");
  3. newComma.className = 'cd';
  4. newComma.innerHTML = '<li class="s"></li>';
  5. div.insertBefore(newComma, div.firstChild);
  6. }


powyzszy kod mianowicie dodaje mi li s czyli dwukropek jako 2 miejsce a ja chce pomiedzy. Dla przyklady godzina 13:56 uzywajac tego skryptu renderuje mi 1:356 teraz probowalem wszystkiego zeby zmienic pozycje dwukropka ale nie wychodzi jakies pomysly questionmark.gif? szkolahtml.pl/day.php



Edit: Udalosie przeniesc dwukropek w porzadane miejsce teraz jaki problem mam to 3 rzad zaczyna odliczanie od 00,99,98,97 itd jest jakis sposob zeby zaczac od 60 ?

Temat zamkniety znalazlem odpowiedz na temat odliczania podaje rozwiazanie dla tych ktorzy keidykolwiek beda potrzebowali odliczania z wieksza mozliwoscia dostosowania do rezultatu


W index.php czy tam html

  1. <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
  2. <script type="text/javascript" src="js/flipcounter.js"></script>


dalej przed tagiem konczacym body
  1.  
  2. <div id="opening"></div>
  3.  
  4. <script type="text/javascript">
  5. $(function(){
  6. $('#opening').countdown({
  7. image:'digits2.png',
  8. startTime:'<? echo date('H:i:s', $closingdown) ?>'
  9. });
  10. });
  11.  
  12.  



js/flipcounter.js

  1.  
  2. /*
  3. * jquery-counter plugin
  4. *
  5. * Copyright (c) 2009 Martin Conte Mac Donell <Reflejo@gmail.com>
  6. * Dual licensed under the MIT and GPL licenses.
  7. *
  8. * <a href="http://docs.jquery.com/License" target="_blank">http://docs.jquery.com/License</a>
  9. *
  10. * IE fix by Andrea Cardinale <a.cardinale@webandtech.it> [23 September 2009]
  11. * IE fix added by Giguashvili, Levan <levangig@gmail.com> [04 April 2011]
  12. */
  13.  
  14. jQuery.fn.countdown = function(userOptions)
  15. {
  16. // Default options
  17. var options = {
  18. stepTime: 60,
  19. // startTime and format MUST follow the same format.
  20. // also you cannot specify a format unordered (e.g. hh:ss:mm is wrong) tutaj ustawiamy sobie format
  21. format: "hh:mm:ss",
  22. startTime: "12:32:55",
  23. digitImages: 6,
  24. digitWidth: 21,
  25. digitHeight: 32,
  26. timerEnd: function(){},
  27. image: "digits2.png"
  28. };
  29. var digits = [], interval;
  30.  
  31. // Draw digits in given container
  32. var createDigits = function(where)
  33. {
  34. var c = 0;
  35.  
  36. options.startTime = options.startTime.split('');
  37. options.format = options.format.split('');
  38.  
  39. var tempStartTime = options.startTime;
  40. var tempFormat = options.format;
  41. // Iterate each startTime digit, if it is not a digit
  42. // we'll asume that it's a separator
  43. for (var i = 0; i < tempStartTime.length; i++)
  44. {
  45. if (parseInt(tempStartTime.charAt(i)) >= 0)
  46. {
  47. elem = jQuery('<div id="cnt_' + i + '" class="cntDigit" />').css({
  48. height: options.digitHeight * options.digitImages * 10,
  49. float: 'left', background: 'url(\'' + options.image + '\')',
  50. width: options.digitWidth});
  51. digits.push(elem);
  52. margin(c, -((parseInt(tempStartTime.charAt(i)) * options.digitHeight *
  53. options.digitImages)));
  54. digits[c].__max = 9;
  55. // Add max digits, for example, first digit of minutes (mm) has
  56. // a max of 5. Conditional max is used when the left digit has reach
  57. // the max. For example second "hours" digit has a conditional max of 4
  58. switch (tempFormat.charAt(i)) {
  59. case 'h':
  60. digits[c].__max = (c % 2 == 0) ? 2: 9;
  61. if (c % 2 == 0)
  62. digits[c].__condmax = 4;
  63. break;
  64. case 'd':
  65. digits[c].__max = 9;
  66. break;
  67. case 'm':
  68. case 's':
  69. digits[c].__max = (c % 2 == 0) ? 5: 9;
  70. }
  71. ++c;
  72. }
  73. else
  74. elem = jQuery('<div class="cntSeparator"/>').css({float: 'left'})
  75. .text(tempStartTime.charAt(i));
  76.  
  77. where.append('<div>');
  78. where.append(elem);
  79. where.append('</div>');
  80. }
  81. };
  82.  
  83. // Set or get element margin
  84. var margin = function(elem, val)
  85. {
  86. if (val !== undefined)
  87. return digits[elem].css({'marginTop': val + 'px'});
  88.  
  89. return parseInt(digits[elem].css('marginTop').replace('px', ''));
  90. };
  91.  
  92. // Makes the movement. This is done by "digitImages" steps.
  93. var moveStep = function(elem)
  94. {
  95. digits[elem]._digitInitial = -(digits[elem].__max * options.digitHeight * options.digitImages);
  96. return function _move() {
  97. mtop = margin(elem) + options.digitHeight;
  98. if (mtop == options.digitHeight) {
  99. margin(elem, digits[elem]._digitInitial);
  100. if (elem > 0) moveStep(elem - 1)();
  101. else
  102. {
  103. clearInterval(interval);
  104. for (var i=0; i < digits.length; i++) margin(i, 0);
  105. options.timerEnd();
  106. return;
  107. }
  108. if ((elem > 0) && (digits[elem].__condmax !== undefined) &&
  109. (digits[elem - 1]._digitInitial == margin(elem - 1)))
  110. margin(elem, -(digits[elem].__condmax * options.digitHeight * options.digitImages));
  111. return;
  112. }
  113.  
  114. margin(elem, mtop);
  115. if (margin(elem) / options.digitHeight % options.digitImages != 0)
  116. setTimeout(_move, options.stepTime);
  117.  
  118. if (mtop == 0) digits[elem].__ismax = true;
  119. }
  120. };
  121.  
  122. jQuery.extend(options, userOptions);
  123. this.css({height: options.digitHeight, overflow: 'hidden'});
  124. createDigits(this);
  125. interval = setInterval(moveStep(digits.length - 1), 1000);
  126. };
  127.  
  128. jQuery.fn.countdown = function(userOptions)
  129. {
  130. // Default options
  131. var options = {
  132. stepTime: 60,
  133. // startTime and format MUST follow the same format.
  134. // also you cannot specify a format unordered (e.g. hh:ss:mm is wrong)
  135. format: "dd:hh:mm:ss",
  136. startTime: "01:12:32:55",
  137. digitImages: 6,
  138. digitWidth: 21,
  139. digitHeight: 32,
  140. timerEnd: function(){},
  141. image: "digits.png"
  142. };
  143. var digits = [], interval;
  144.  
  145. // Draw digits in given container
  146. var createDigits = function(where)
  147. {
  148. var c = 0;
  149. var tempStartTime = options.startTime;
  150. // Iterate each startTime digit, if it is not a digit
  151. // we'll asume that it's a separator
  152. for (var i = 0; i < options.startTime.length; i++)
  153. {
  154. if (parseInt(tempStartTime.charAt(i)) >= 0)
  155. {
  156. elem = jQuery('<div id="cnt_' + i + '" class="cntDigit" />').css({
  157. height: options.digitHeight * options.digitImages * 10,
  158. float: 'left', background: 'url(\'' + options.image + '\')',
  159. width: options.digitWidth});
  160. digits.push(elem);
  161. margin(c, -((parseInt(tempStartTime.charAt(i)) * options.digitHeight *
  162. options.digitImages)));
  163. digits[c].__max = 9;
  164. // Add max digits, for example, first digit of minutes (mm) has
  165. // a max of 5. Conditional max is used when the left digit has reach
  166. // the max. For example second "hours" digit has a conditional max of 4
  167. switch (options.format[i]) {
  168. case 'h':
  169. digits[c].__max = (c % 2 == 0) ? 2: 9;
  170. if (c % 2 == 0)
  171. digits[c].__condmax = 4;
  172. break;
  173. case 'd':
  174. digits[c].__max = 9;
  175. break;
  176. case 'm':
  177. case 's':
  178. digits[c].__max = (c % 2 == 0) ? 5: 9;
  179. }
  180. ++c;
  181. }
  182. else
  183. elem = jQuery('<div class="cntSeparator"/>').css({float: 'left'})
  184. .text(tempStartTime.charAt(i));
  185.  
  186. where.append('<div>');
  187. where.append(elem);
  188. where.append('</div>');
  189. }
  190. };
  191.  
  192. // Set or get element margin
  193. var margin = function(elem, val)
  194. {
  195. if (val !== undefined)
  196. return digits[elem].css({'marginTop': val + 'px'});
  197.  
  198. return parseInt(digits[elem].css('marginTop').replace('px', ''));
  199. };
  200.  
  201. // Makes the movement. This is done by "digitImages" steps.
  202. var moveStep = function(elem)
  203. {
  204. digits[elem]._digitInitial = -(digits[elem].__max * options.digitHeight * options.digitImages);
  205. return function _move() {
  206. mtop = margin(elem) + options.digitHeight;
  207. if (mtop == options.digitHeight) {
  208. margin(elem, digits[elem]._digitInitial);
  209. if (elem > 0) moveStep(elem - 1)();
  210. else
  211. {
  212. clearInterval(interval);
  213. for (var i=0; i < digits.length; i++) margin(i, 0);
  214. options.timerEnd();
  215. return;
  216. }
  217. if ((elem > 0) && (digits[elem].__condmax !== undefined) &&
  218. (digits[elem - 1]._digitInitial == margin(elem - 1)))
  219. margin(elem, -(digits[elem].__condmax * options.digitHeight * options.digitImages));
  220. return;
  221. }
  222.  
  223. margin(elem, mtop);
  224. if (margin(elem) / options.digitHeight % options.digitImages != 0)
  225. setTimeout(_move, options.stepTime);
  226.  
  227. if (mtop == 0) digits[elem].__ismax = true;
  228. }
  229. };
  230.  
  231. jQuery.extend(options, userOptions);
  232. this.css({height: options.digitHeight, overflow: 'hidden'});
  233. createDigits(this);
  234. interval = setInterval(moveStep(digits.length - 1), 1000);
  235. };
  236.  
  237.  
  238.  
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.