PHP - Timer
How am i able to setup a countdown timer?
I use the time(); function and want to compare that time with another time that is 8 hours ahead to form a countdown timer, how am i able to do this using php? Similar TutorialsCan someone show me the way? I am creating a online examination with time pressured. For example Part I of examination is only good for 20 mins. now if 20 mins is over it will be lock or good as submit. How will I do that? tnx everyone. Hey Guy's
I need a quick fix to solve an urgent problem..
I used to do some coding years ago but the old grey matter is not so responsive these days and I have been up all night trying to resolve a problem.
I have built a website for my daughters and created a simple web form but I have come unstuck. All those skills I once took for granted are gone.
So here's where I am at. I've created an input form_mail.html, a contact.php and a reply.html page uploaded them to the Server and hit an error when I tried to send a message. The problem it seems is with the PHP.ini File.
In short I don't have one Duh! Yes I know I should go back and study and that much I intend to do I am currently installing xxampp, MySQL and php on my new laptop.
But in the meantime I need your help urgently..
What code do I need in the php.ini file to make the Form Mail work?
Is there somewhere on the web where I can see a simple code, I am just too tired to wade through heavy docs right now.
Your help would be much appreciated at this time.
Sincerely
Tam
Hello. Well i'm trying to make a timer to execute some code. Basically I want 3 minutes, every 3 minutes a piece of code is activated, then another 3 minutes later it does so again. But I have tried different ways of doing this, I tried with cookies and found out the person with the cookie is the only one that can refresh the page to activate the code. Same with sessions. And all the others are usually interrupted and start the timer again when the page reset. So yeah, I don't want it to be interrupted by refreshing. So any help please? I also tried with timestamps, this has proven to be the best way but I can never get it working 100% Can anyone help me to make a countdown timer in php? so basically i've made a quiz.. but i want it to have a time of 1 hour 30 mins.. but i dont have any idea of how to make a timer I don't know if this can be done in PHP, but what I'm wanting to do is calculate the amount of time a user has spent in a session. I am planning on making a script that allows users to race through questions and the person who does it the fastest is the winner. Could someone help me out with this? Thanks Hi im trying to Make a Timer count down in this code function getTime($host, $user, $pass, $db, $hours, $ip, $serverId) { $c = @mysql_connect($host, $user, $pass); if(mysql_select_db($db, $c)) { $dateQ = mysql_query('SELECT MAX(date) AS lastVoteDate FROM `'. $db .'`.`votes` WHERE ip="'. $ip .'"') or die(mysql_error()); $getDate = mysql_fetch_assoc($dateQ); $diffrence = time() - strtotime($getDate['lastVoteDate']); $hours = number_format(($diffrence / 60 / 60)); $minutes = number_format(($hours / 60)); if(!$hours < 1){ return ("". $hours .""); } else { return ("0"); } } else { return array(1, "Error connecting to DB."); } } Hi, I'm new to the forum, I have a slight issue with my script. Basically, I need to figure out a script that will allow me to do the following: - The user defines x amount of ours to count down (ie. 16 hours, 8 hours, 4 hours) - The script then starts the count down of the x hours - I need the script to count between 08:00 - 20:00 after 20:00 it needs to stop, then start again at 08:00 - The script also need to stop throughout the weekend (ie counts until 20:00 on friday, stops saturday & sunday, then starts 08:00 on monday) Can this be done? If so, any pointers or help? Many thanks, Lewis. I am currently in the process of creating an online time based game. One of the big things is that users get gold every half an hour :00 & :30. What I am trying to do is offer a physical clock so that users can see how much time is left before they more more gold. I have been trying to do this using PHP. All I have currently is the time, but would like something like "$minutes until more gold". This is currently what I have now: (added 1 hour for EST.) <?php echo date("h:i A",time()+60*60); ?> Hi all, I would like to create a timer showing a countdown from 0% to 100% (representing "power") ending at noon GMT on Sunday 4th September. The countdown started a few weeks ago, hence as of 1pm GMT on Tuesday 1st March, the countdown should be at 15% The "power" increases by 5% every 11 days so by my calculations, the percentage in our countdown should increase by 0.01% every 1900.8 seconds. So, with this theory, how would I go about coding it? I guess since I use GMT, I need to take a timestamp in GMT first, then calculate the difference between this and Sunday 4th September at 12.00pm. I am not sure though which format of time/date is best to do the calculation. Would it be better to use Unix time? Any help, suggestions and/or code is greatly appreciated. Skulty I am looking for a tutorial or advice wherein I can create a timer for my auction items. You can see those timers on the site like quibids.com The concept is like this. I post and auction and set an end time for the auction. When the auction is live, The timer will start and will show how many hours and seconds are remaining. If any one has a class or some link to a tutorial site, that'll be a great help. Thanks a lot. Here's the code for the countdown timer. It works. At the end of the countdown, it's suppose to show the message "EXPIRED". But that message only shows once I reload the page. The countdown itself stops at 00:00:01. Is there a way to automatically show the message after that, instead of reloading the page to show it? <style> div#counter{ margin: 100px auto; width: 305px; padding:20px; border:1px solid #000000; } div#counter span{ background-color: #00CAF6; padding:5px; margin:1px; font-size:30px; } </style> <?php $target_date = '2019-01-12 05:40:00'; $timeLeft = (strtotime($target_date) - time()) * 1000; ?> <script src="javascripts/timer.js"></script> <script> $(document).ready(function(){ var timeLeft = <?php echo $timeLeft ; ?>; var timer = new Timer($('#counter'), timeLeft); if (timeLeft <= 0) { $('#counter').text('EXPIRED'); } }); </script> <div id="counter"> <span class="hour">00</span> <span class="min">00</span> <span class="sec">00</span> </div> Edited January 11, 2019 by imgrooot Can anyone help me with a 60 second timer for my website. I want to display the following text to my customers at the top of the page: "You will be automatically redirected to homepage in [timer]" In my header I have the following code: <meta http-equiv="refresh" content="61;url=home.php"> Ok guys, I'm gonna have to throw a question out there. I am building a mmorpg. And I could use some help with some coding. I have two cron files that run every hour and every day(24 hrs ) My question is, how can I add a visual countdown based on server time to show how long the cron has left to tick? I have tried many countdowns but most of them reset once I leave the page. So basically I need a countdown timer to show the user how long till he getts more food or whatever. Any help would be greatly appreciated. Thank you in advance for taking time to read and/or help. Is it possible to set a certain session to time out after say 2 minutes. I want to lock a person from seeing a page for 2 minutes then destroy the session yet keep the primary session and still keep dpoing what they were doing? Just curious. Thanks, Hey guys and gals!
I am currently working on implementing the following functionality in one of my pages:
Whenever a person with a specific IP address visits the page, an internal countdown timer of 2 hours should be started. Until that timer is active, the only response from the page ANYONE can get would be a predefined echo value. Once the timer has run out, the normal script execution of the rest of the page should be restored.
Any pointers and tips on how to approach that would be greatly appreciated.
Hi friends,
I have this script for countdown Timer, and when i open my site the timer show Nan, can you help me please
/* http://keith-wood.name/countdown.html Countdown for jQuery v2.0.0. Written by Keith Wood (kbwood{at}iinet.com.au) January 2008. Available under the MIT (https://github.com/jquery/jquery/blob/master/MIT-LICENSE.txt) license. Please attribute the author if you use it. */ (function($) { // Hide scope, no $ conflict var pluginName = 'countdown'; var Y = 0; // Years var O = 1; // Months var W = 2; // Weeks var D = 3; // Days var H = 4; // Hours var M = 5; // Minutes var S = 6; // Seconds /** Create the countdown plugin. <p>Sets an element to show the time remaining until a given instant.</p> <p>Expects HTML like:</p> <pre><div></div></pre> <p>Provide inline configuration like:</p> <pre><div data-countdown="name: 'value'"></div></pre> @module Countdown @augments JQPlugin @example $(selector).countdown({until: +300}) */ $.JQPlugin.createPlugin({ /** The name of the plugin. */ name: pluginName, /** Countdown expiry callback. Triggered when the countdown expires. @callback expiryCallback */ /** Countdown server synchronisation callback. Triggered when the countdown is initialised. @callback serverSyncCallback @return {Date} The current date/time on the server as expressed in the local timezone. */ /** Countdown tick callback. Triggered on every <code>tickInterval</code> ticks of the countdown. @callback tickCallback @param periods {number[]} The breakdown by period (years, months, weeks, days, hours, minutes, seconds) of the time remaining/passed. */ /** Countdown which labels callback. Triggered when the countdown is being display to determine which set of labels (<code>labels</code>, <code>labels1</code>, ...) are to be used for the current period value. @callback whichLabelsCallback @param num {number} The current period value. @return {number} The suffix for the label set to use. */ /** Default settings for the plugin. @property until {Date|number|string} The date/time to count down to, or number of seconds offset from now, or string of amounts and units for offset(s) from now: 'Y' years, 'O' months, 'W' weeks, 'D' days, 'H' hours, 'M' minutes, 'S' seconds. @example until: new Date(2013, 12-1, 25, 13, 30) until: +300 until: '+1O -2D' @property [since] {Date|number|string} The date/time to count up from, or number of seconds offset from now, or string for unit offset(s): 'Y' years, 'O' months, 'W' weeks, 'D' days, 'H' hours, 'M' minutes, 'S' seconds. @example since: new Date(2013, 1-1, 1) since: -300 since: '-1O +2D' @property [timezone=null] {number} The timezone (hours or minutes from GMT) for the target times, or null for client local timezone. @example timezone: +10 timezone: -60 @property [serverSync=null] {serverSyncCallback} A function to retrieve the current server time for synchronisation. @property [format='dHMS'] {string} The format for display - upper case for always, lower case only if non-zero, 'Y' years, 'O' months, 'W' weeks, 'D' days, 'H' hours, 'M' minutes, 'S' seconds. @property [layout=''] {string} Build your own layout for the countdown. @example layout: '{d<}{dn} {dl}{d>} {hnn}:{mnn}:{snn}' @property [compact=false] {boolean} True to display in a compact format, false for an expanded one. @property [padZeroes=false] {boolean} True to add leading zeroes @property [significant=0] {number} The number of periods with non-zero values to show, zero for all. @property [description=''] {string} The description displayed for the countdown. @property [expiryUrl=''] {string} A URL to load upon expiry, replacing the current page. @property [expiryText=''] {string} Text to display upon expiry, replacing the countdown. This may be HTML. @property [alwaysExpire=false] {boolean} True to trigger <code>onExpiry</code> even if target time has passed. @property [onExpiry=null] {expiryCallback} Callback when the countdown expires - receives no parameters and <code>this</code> is the containing division. @example onExpiry: function() { ... } @property [onTick=null] {tickCallback} Callback when the countdown is updated - receives <code>number[7]</code> being the breakdown by period (years, months, weeks, days, hours, minutes, seconds - based on <code>format</code>) and <code>this</code> is the containing division. @example onTick: function(periods) { var secs = $.countdown.periodsToSeconds(periods); if (secs < 300) { // Last five minutes ... } } @property [tickInterval=1] {number} The interval (seconds) between <code>onTick</code> callbacks. */ defaultOptions: { until: null, since: null, timezone: null, serverSync: null, format: 'dHMS', layout: '', compact: false, padZeroes: false, significant: 0, description: '', expiryUrl: '', expiryText: '', alwaysExpi false, onExpiry: null, onTick: null, tickInterval: 1 }, /** Localisations for the plugin. Entries are objects indexed by the language code ('' being the default US/English). Each object has the following attributes. @property [labels=['Years','Months','Weeks','Days','Hours','Minutes','Seconds']] {string[]} The display texts for the counter periods. @property [labels1=['Year','Month','Week','Day','Hour','Minute','Second']] {string[]} The display texts for the counter periods if they have a value of 1. Add other <code>labels<em>n</em></code> attributes as necessary to cater for other numeric idiosyncrasies of the localisation. @property [compactLabels=['y','m','w','d']] {string[]} The compact texts for the counter periods. @property [whichLabels=null] {whichLabelsCallback} A function to determine which <code>labels<em>n</em></code> to use. @example whichLabels: function(num) { return (num > 1 ? 0 : 1); } @property [digits=['0','1',...,'9']] {number[]} The digits to display (0-9). @property [timeSeparator=':'] {string} Separator for time periods in the compact layout. @property [isRTL=false] {boolean} True for right-to-left languages, false for left-to-right. */ regionalOptions: { // Available regional settings, indexed by language/country code '': { // Default regional settings - English/US labels: ['Years', 'Months', 'Weeks', 'Days', 'Hours', 'Minutes', 'Seconds'], labels1: ['Year', 'Month', 'Week', 'Day', 'Hour', 'Minute', 'Second'], compactLabels: ['y', 'm', 'w', 'd'], whichLabels: null, digits: ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'], timeSeparator: ':', isRTL: false } }, /** Names of getter methods - those that can't be chained. */ _getters: ['getTimes'], /* Class name for the right-to-left marker. */ _rtlClass: pluginName + '-rtl', /* Class name for the countdown section marker. */ _sectionClass: pluginName + '-section', /* Class name for the period amount marker. */ _amountClass: pluginName + '-amount', /* Class name for the period name marker. */ _periodClass: pluginName + '-period', /* Class name for the countdown row marker. */ _rowClass: pluginName + '-row', /* Class name for the holding countdown marker. */ _holdingClass: pluginName + '-holding', /* Class name for the showing countdown marker. */ _showClass: pluginName + '-show', /* Class name for the description marker. */ _descrClass: pluginName + '-descr', /* List of currently active countdown elements. */ _timerElems: [], /** Additional setup for the countdown. Apply default localisations. Create the timer. */ _init: function() { var self = this; this._super(); this._serverSyncs = []; var now = (typeof Date.now == 'function' ? Date.now : function() { return new Date().getTime(); }); var perfAvail = (window.performance && typeof window.performance.now == 'function'); // Shared timer for all countdowns function timerCallBack(timestamp) { var drawStart = (timestamp < 1e12 ? // New HTML5 high resolution timer (perfAvail ? (performance.now() + performance.timing.navigationStart) : now()) : // Integer milliseconds since unix epoch timestamp || now()); if (drawStart - animationStartTime >= 1000) { self._updateElems(); animationStartTime = drawStart; } requestAnimationFrame(timerCallBack); } var requestAnimationFrame = window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame || null; // This is when we expect a fall-back to setInterval as it's much more fluid var animationStartTime = 0; if (!requestAnimationFrame || $.noRequestAnimationFrame) { $.noRequestAnimationFrame = null; setInterval(function() { self._updateElems(); }, 980); // Fall back to good old setInterval } else { animationStartTime = window.animationStartTime || window.webkitAnimationStartTime || window.mozAnimationStartTime || window.oAnimationStartTime || window.msAnimationStartTime || now(); requestAnimationFrame(timerCallBack); } }, /** Convert a date/time to UTC. @param tz {number} The hour or minute offset from GMT, e.g. +9, -360. @param year {Date|number} the date/time in that timezone or the year in that timezone. @param [month] {number} The month (0 - 11) (omit if <code>year</code> is a <code>Date</code>). @param [day] {number} The day (omit if <code>year</code> is a <code>Date</code>). @param [hours] {number} The hour (omit if <code>year</code> is a <code>Date</code>). @param [mins] {number} The minute (omit if <code>year</code> is a <code>Date</code>). @param [secs] {number} The second (omit if <code>year</code> is a <code>Date</code>). @param [ms] {number} The millisecond (omit if <code>year</code> is a <code>Date</code>). @return {Date} The equivalent UTC date/time. @example $.countdown.UTCDate(+10, 2013, 12-1, 25, 12, 0) $.countdown.UTCDate(-7, new Date(2013, 12-1, 25, 12, 0)) */ UTCDate: function(tz, year, month, day, hours, mins, secs, ms) { if (typeof year == 'object' && year.constructor == Date) { ms = year.getMilliseconds(); secs = year.getSeconds(); mins = year.getMinutes(); hours = year.getHours(); day = year.getDate(); month = year.getMonth(); year = year.getFullYear(); } var d = new Date(); d.setUTCFullYear(year); d.setUTCDate(1); d.setUTCMonth(month || 0); d.setUTCDate(day || 1); d.setUTCHours(hours || 0); d.setUTCMinutes((mins || 0) - (Math.abs(tz) < 30 ? tz * 60 : tz)); d.setUTCSeconds(secs || 0); d.setUTCMilliseconds(ms || 0); return d; }, /** Convert a set of periods into seconds. Averaged for months and years. @param periods {number[]} The periods per year/month/week/day/hour/minute/second. @return {number} The corresponding number of seconds. @example var secs = $.countdown.periodsToSeconds(periods) */ periodsToSeconds: function(periods) { return periods[0] * 31557600 + periods[1] * 2629800 + periods[2] * 604800 + periods[3] * 86400 + periods[4] * 3600 + periods[5] * 60 + periods[6]; }, _instSettings: function(elem, options) { return {_periods: [0, 0, 0, 0, 0, 0, 0]}; }, /** Add an element to the list of active ones. @private @param elem {Element} The countdown element. */ _addElem: function(elem) { if (!this._hasElem(elem)) { this._timerElems.push(elem); } }, /** See if an element is in the list of active ones. @private @param elem {Element} The countdown element. @return {boolean} True if present, false if not. */ _hasElem: function(elem) { return ($.inArray(elem, this._timerElems) > -1); }, /** Remove an element from the list of active ones. @private @param elem {Element} The countdown element. */ _removeElem: function(elem) { this._timerElems = $.map(this._timerElems, function(value) { return (value == elem ? null : value); }); // delete entry }, /** Update each active timer element. @private */ _updateElems: function() { for (var i = this._timerElems.length - 1; i >= 0; i--) { this._updateCountdown(this._timerElems[i]); } }, _optionsChanged: function(elem, inst, options) { if (options.layout) { options.layout = options.layout.replace(/</g, '<').replace(/>/g, '>'); } this._resetExtraLabels(inst.options, options); var timezoneChanged = (inst.options.timezone != options.timezone); $.extend(inst.options, options); this._adjustSettings(elem, inst, options.until != null || options.since != null || timezoneChanged); var now = new Date(); if ((inst._since && inst._since < now) || (inst._until && inst._until > now)) { this._addElem(elem[0]); } this._updateCountdown(elem, inst); }, /** Redisplay the countdown with an updated display. @private @param elem {Element|jQuery} The containing division. @param inst {object} The current settings for this instance. */ _updateCountdown: function(elem, inst) { elem = elem.jquery ? elem : $(elem); inst = inst || elem.data(this.name); if (!inst) { return; } elem.html(this._generateHTML(inst)).toggleClass(this._rtlClass, inst.options.isRTL); if ($.isFunction(inst.options.onTick)) { var periods = inst._hold != 'lap' ? inst._periods : this._calculatePeriods(inst, inst._show, inst.options.significant, new Date()); if (inst.options.tickInterval == 1 || this.periodsToSeconds(periods) % inst.options.tickInterval == 0) { inst.options.onTick.apply(elem[0], [periods]); } } var expired = inst._hold != 'pause' && (inst._since ? inst._now.getTime() < inst._since.getTime() : inst._now.getTime() >= inst._until.getTime()); if (expired && !inst._expiring) { inst._expiring = true; if (this._hasElem(elem[0]) || inst.options.alwaysExpire) { this._removeElem(elem[0]); if ($.isFunction(inst.options.onExpiry)) { inst.options.onExpiry.apply(elem[0], []); } if (inst.options.expiryText) { var layout = inst.options.layout; inst.options.layout = inst.options.expiryText; this._updateCountdown(elem[0], inst); inst.options.layout = layout; } if (inst.options.expiryUrl) { window.location = inst.options.expiryUrl; } } inst._expiring = false; } else if (inst._hold == 'pause') { this._removeElem(elem[0]); } }, /** Reset any extra labelsn and compactLabelsn entries if changing labels. @private @param base {object} The options to be updated. @param options {object} The new option values. */ _resetExtraLabels: function(base, options) { var changingLabels = false; for (var n in options) { if (n != 'whichLabels' && n.match(/[Ll]abels/)) { changingLabels = true; break; } } if (changingLabels) { for (var n in base) { // Remove custom numbered labels if (n.match(/[Ll]abels[02-9]|compactLabels1/)) { base[n] = null; } } } }, /** Calculate internal settings for an instance. @private @param elem {jQuery} The containing division. @param inst {object} The current settings for this instance. @param recalc {boolean} True if until or since are set. */ _adjustSettings: function(elem, inst, recalc) { var now; var serverOffset = 0; var serverEntry = null; for (var i = 0; i < this._serverSyncs.length; i++) { if (this._serverSyncs[i][0] == inst.options.serverSync) { serverEntry = this._serverSyncs[i][1]; break; } } if (serverEntry != null) { serverOffset = (inst.options.serverSync ? serverEntry : 0); now = new Date(); } else { var serverResult = ($.isFunction(inst.options.serverSync) ? inst.options.serverSync.apply(elem[0], []) : null); now = new Date(); serverOffset = (serverResult ? now.getTime() - serverResult.getTime() : 0); this._serverSyncs.push([inst.options.serverSync, serverOffset]); } var timezone = inst.options.timezone; timezone = (timezone == null ? -now.getTimezoneOffset() : timezone); if (recalc || (!recalc && inst._until == null && inst._since == null)) { inst._since = inst.options.since; if (inst._since != null) { inst._since = this.UTCDate(timezone, this._determineTime(inst._since, null)); if (inst._since && serverOffset) { inst._since.setMilliseconds(inst._since.getMilliseconds() + serverOffset); } } inst._until = this.UTCDate(timezone, this._determineTime(inst.options.until, now)); if (serverOffset) { inst._until.setMilliseconds(inst._until.getMilliseconds() + serverOffset); } } inst._show = this._determineShow(inst); }, /** Remove the countdown widget from a div. @param elem {jQuery} The containing division. @param inst {object} The current instance object. */ _preDestroy: function(elem, inst) { this._removeElem(elem[0]); elem.empty(); }, /** Pause a countdown widget at the current time. Stop it running but remember and display the current time. @param elem {Element} The containing division. @example $(selector).countdown('pause') */ pause: function(elem) { this._hold(elem, 'pause'); }, /** Pause a countdown widget at the current time. Stop the display but keep the countdown running. @param elem {Element} The containing division. @example $(selector).countdown('lap') */ lap: function(elem) { this._hold(elem, 'lap'); }, /** Resume a paused countdown widget. @param elem {Element} The containing division. @example $(selector).countdown('resume') */ resume: function(elem) { this._hold(elem, null); }, /** Toggle a paused countdown widget. @param elem {Element} The containing division. @example $(selector).countdown('toggle') */ toggle: function(elem) { var inst = $.data(elem, this.name) || {}; this[!inst._hold ? 'pause' : 'resume'](elem); }, /** Toggle a lapped countdown widget. @param elem {Element} The containing division. @example $(selector).countdown('toggleLap') */ toggleLap: function(elem) { var inst = $.data(elem, this.name) || {}; this[!inst._hold ? 'lap' : 'resume'](elem); }, /** Pause or resume a countdown widget. @private @param elem {Element} The containing division. @param hold {string} The new hold setting. */ _hold: function(elem, hold) { var inst = $.data(elem, this.name); if (inst) { if (inst._hold == 'pause' && !hold) { inst._periods = inst._savePeriods; var sign = (inst._since ? '-' : '+'); inst[inst._since ? '_since' : '_until'] = this._determineTime(sign + inst._periods[0] + 'y' + sign + inst._periods[1] + 'o' + sign + inst._periods[2] + 'w' + sign + inst._periods[3] + 'd' + sign + inst._periods[4] + 'h' + sign + inst._periods[5] + 'm' + sign + inst._periods[6] + 's'); this._addElem(elem); } inst._hold = hold; inst._savePeriods = (hold == 'pause' ? inst._periods : null); $.data(elem, this.name, inst); this._updateCountdown(elem, inst); } }, /** Return the current time periods. @param elem {Element} The containing division. @return {number[]} The current periods for the countdown. @example var periods = $(selector).countdown('getTimes') */ getTimes: function(elem) { var inst = $.data(elem, this.name); return (!inst ? null : (inst._hold == 'pause' ? inst._savePeriods : (!inst._hold ? inst._periods : this._calculatePeriods(inst, inst._show, inst.options.significant, new Date())))); }, /** A time may be specified as an exact value or a relative one. @private @param setting {string|number|Date} The date/time value as a relative or absolute value. @param defaultTime {Date} The date/time to use if no other is supplied. @return {Date} The corresponding date/time. */ _determineTime: function(setting, defaultTime) { var self = this; var offsetNumeric = function(offset) { // e.g. +300, -2 var time = new Date(); time.setTime(time.getTime() + offset * 1000); return time; }; var offsetString = function(offset) { // e.g. '+2d', '-4w', '+3h +30m' offset = offset.toLowerCase(); var time = new Date(); var year = time.getFullYear(); var month = time.getMonth(); var day = time.getDate(); var hour = time.getHours(); var minute = time.getMinutes(); var second = time.getSeconds(); var pattern = /([+-]?[0-9]+)\s*(s|m|h|d|w|o|y)?/g; var matches = pattern.exec(offset); while (matches) { switch (matches[2] || 's') { case 's': second += parseInt(matches[1], 10); break; case 'm': minute += parseInt(matches[1], 10); break; case 'h': hour += parseInt(matches[1], 10); break; case 'd': day += parseInt(matches[1], 10); break; case 'w': day += parseInt(matches[1], 10) * 7; break; case 'o': month += parseInt(matches[1], 10); day = Math.min(day, self._getDaysInMonth(year, month)); break; case 'y': year += parseInt(matches[1], 10); day = Math.min(day, self._getDaysInMonth(year, month)); break; } matches = pattern.exec(offset); } return new Date(year, month, day, hour, minute, second, 0); }; var time = (setting == null ? defaultTime : (typeof setting == 'string' ? offsetString(setting) : (typeof setting == 'number' ? offsetNumeric(setting) : setting))); if (time) time.setMilliseconds(0); return time; }, /** Determine the number of days in a month. @private @param year {number} The year. @param month {number} The month. @return {number} The days in that month. */ _getDaysInMonth: function(year, month) { return 32 - new Date(year, month, 32).getDate(); }, /** Default implementation to determine which set of labels should be used for an amount. Use the <code>labels</code> attribute with the same numeric suffix (if it exists). @private @param num {number} The amount to be displayed. @return {number} The set of labels to be used for this amount. */ _normalLabels: function(num) { return num; }, /** Generate the HTML to display the countdown widget. @private @param inst {object} The current settings for this instance. @return {string} The new HTML for the countdown display. */ _generateHTML: function(inst) { var self = this; // Determine what to show inst._periods = (inst._hold ? inst._periods : this._calculatePeriods(inst, inst._show, inst.options.significant, new Date())); // Show all 'asNeeded' after first non-zero value var shownNonZero = false; var showCount = 0; var sigCount = inst.options.significant; var show = $.extend({}, inst._show); for (var period = Y; period <= S; period++) { shownNonZero |= (inst._show[period] == '?' && inst._periods[period] > 0); show[period] = (inst._show[period] == '?' && !shownNonZero ? null : inst._show[period]); showCount += (show[period] ? 1 : 0); sigCount -= (inst._periods[period] > 0 ? 1 : 0); } var showSignificant = [false, false, false, false, false, false, false]; for (var period = S; period >= Y; period--) { // Determine significant periods if (inst._show[period]) { if (inst._periods[period]) { showSignificant[period] = true; } else { showSignificant[period] = sigCount > 0; sigCount--; } } } var labels = (inst.options.compact ? inst.options.compactLabels : inst.options.labels); var whichLabels = inst.options.whichLabels || this._normalLabels; var showCompact = function(period) { var labelsNum = inst.options['compactLabels' + whichLabels(inst._periods[period])]; return (show[period] ? self._translateDigits(inst, inst._periods[period]) + (labelsNum ? labelsNum[period] : labels[period]) + ' ' : ''); }; var minDigits = (inst.options.padZeroes ? 2 : 1); var showFull = function(period) { var labelsNum = inst.options['labels' + whichLabels(inst._periods[period])]; return ((!inst.options.significant && show[period]) || (inst.options.significant && showSignificant[period]) ? '<span class="' + self._sectionClass + '">' + '<span class="' + self._amountClass + '">' + self._minDigits(inst, inst._periods[period], minDigits) + '</span>' + '<span class="' + self._periodClass + '">' + (labelsNum ? labelsNum[period] : labels[period]) + '</span></span>' : ''); }; return (inst.options.layout ? this._buildLayout(inst, show, inst.options.layout, inst.options.compact, inst.options.significant, showSignificant) : ((inst.options.compact ? // Compact version '<span class="' + this._rowClass + ' ' + this._amountClass + (inst._hold ? ' ' + this._holdingClass : '') + '">' + showCompact(Y) + showCompact(O) + showCompact(W) + showCompact(D) + (show[H] ? this._minDigits(inst, inst._periods[H], 2) : '') + (show[M] ? (show[H] ? inst.options.timeSeparator : '') + this._minDigits(inst, inst._periods[M], 2) : '') + (show[S] ? (show[H] || show[M] ? inst.options.timeSeparator : '') + this._minDigits(inst, inst._periods[S], 2) : '') : // Full version '<span class="' + this._rowClass + ' ' + this._showClass + (inst.options.significant || showCount) + (inst._hold ? ' ' + this._holdingClass : '') + '">' + showFull(Y) + showFull(O) + showFull(W) + showFull(D) + showFull(H) + showFull(M) + showFull(S)) + '</span>' + (inst.options.description ? '<span class="' + this._rowClass + ' ' + this._descrClass + '">' + inst.options.description + '</span>' : ''))); }, /** Construct a custom layout. @private @param inst {object} The current settings for this instance. @param show {boolean[]} Flags indicating which periods are requested. @param layout {string} The customised layout. @param compact {boolean} True if using compact labels. @param significant {number} The number of periods with values to show, zero for all. @param showSignificant {boolean[]} Other periods to show for significance. @return {string} The custom HTML. */ _buildLayout: function(inst, show, layout, compact, significant, showSignificant) { var labels = inst.options[compact ? 'compactLabels' : 'labels']; var whichLabels = inst.options.whichLabels || this._normalLabels; var labelFor = function(index) { return (inst.options[(compact ? 'compactLabels' : 'labels') + whichLabels(inst._periods[index])] || labels)[index]; }; var digit = function(value, position) { return inst.options.digits[Math.floor(value / position) % 10]; }; var subs = {desc: inst.options.description, sep: inst.options.timeSeparator, yl: labelFor(Y), yn: this._minDigits(inst, inst._periods[Y], 1), ynn: this._minDigits(inst, inst._periods[Y], 2), ynnn: this._minDigits(inst, inst._periods[Y], 3), y1: digit(inst._periods[Y], 1), y10: digit(inst._periods[Y], 10), y100: digit(inst._periods[Y], 100), y1000: digit(inst._periods[Y], 1000), ol: labelFor(O), on: this._minDigits(inst, inst._periods[O], 1), onn: this._minDigits(inst, inst._periods[O], 2), onnn: this._minDigits(inst, inst._periods[O], 3), o1: digit(inst._periods[O], 1), o10: digit(inst._periods[O], 10), o100: digit(inst._periods[O], 100), o1000: digit(inst._periods[O], 1000), wl: labelFor(W), wn: this._minDigits(inst, inst._periods[W], 1), wnn: this._minDigits(inst, inst._periods[W], 2), wnnn: this._minDigits(inst, inst._periods[W], 3), w1: digit(inst._periods[W], 1), w10: digit(inst._periods[W], 10), w100: digit(inst._periods[W], 100), w1000: digit(inst._periods[W], 1000), dl: labelFor(D), dn: this._minDigits(inst, inst._periods[D], 1), dnn: this._minDigits(inst, inst._periods[D], 2), dnnn: this._minDigits(inst, inst._periods[D], 3), d1: digit(inst._periods[D], 1), d10: digit(inst._periods[D], 10), d100: digit(inst._periods[D], 100), d1000: digit(inst._periods[D], 1000), hl: labelFor(H), hn: this._minDigits(inst, inst._periods[H], 1), hnn: this._minDigits(inst, inst._periods[H], 2), hnnn: this._minDigits(inst, inst._periods[H], 3), h1: digit(inst._periods[H], 1), h10: digit(inst._periods[H], 10), h100: digit(inst._periods[H], 100), h1000: digit(inst._periods[H], 1000), ml: labelFor(M), mn: this._minDigits(inst, inst._periods[M], 1), mnn: this._minDigits(inst, inst._periods[M], 2), mnnn: this._minDigits(inst, inst._periods[M], 3), m1: digit(inst._periods[M], 1), m10: digit(inst._periods[M], 10), m100: digit(inst._periods[M], 100), m1000: digit(inst._periods[M], 1000), sl: labelFor(S), sn: this._minDigits(inst, inst._periods[S], 1), snn: this._minDigits(inst, inst._periods[S], 2), snnn: this._minDigits(inst, inst._periods[S], 3), s1: digit(inst._periods[S], 1), s10: digit(inst._periods[S], 10), s100: digit(inst._periods[S], 100), s1000: digit(inst._periods[S], 1000)}; var html = layout; // Replace period containers: {p<}...{p>} for (var i = Y; i <= S; i++) { var period = 'yowdhms'.charAt(i); var re = new RegExp('\\{' + period + '<\\}([\\s\\S]*)\\{' + period + '>\\}', 'g'); html = html.replace(re, ((!significant && show[i]) || (significant && showSignificant[i]) ? '$1' : '')); } // Replace period values: {pn} $.each(subs, function(n, v) { var re = new RegExp('\\{' + n + '\\}', 'g'); html = html.replace(re, v); }); return html; }, /** Ensure a numeric value has at least n digits for display. @private @param inst {object} The current settings for this instance. @param value {number} The value to display. @param len {number} The minimum length. @return {string} The display text. */ _minDigits: function(inst, value, len) { value = '' + value; if (value.length >= len) { return this._translateDigits(inst, value); } value = '0000000000' + value; return this._translateDigits(inst, value.substr(value.length - len)); }, /** Translate digits into other representations. @private @param inst {object} The current settings for this instance. @param value {string} The text to translate. @return {string} The translated text. */ _translateDigits: function(inst, value) { return ('' + value).replace(/[0-9]/g, function(digit) { return inst.options.digits[digit]; }); }, /** Translate the format into flags for each period. @private @param inst {object} The current settings for this instance. @return {string[]} Flags indicating which periods are requested (?) or required (!) by year, month, week, day, hour, minute, second. */ _determineShow: function(inst) { var format = inst.options.format; var show = []; show[Y] = (format.match('y') ? '?' : (format.match('Y') ? '!' : null)); show[O] = (format.match('o') ? '?' : (format.match('O') ? '!' : null)); show[W] = (format.match('w') ? '?' : (format.match('W') ? '!' : null)); show[D] = (format.match('d') ? '?' : (format.match('D') ? '!' : null)); show[H] = (format.match('h') ? '?' : (format.match('H') ? '!' : null)); show[M] = (format.match('m') ? '?' : (format.match('M') ? '!' : null)); show[S] = (format.match('s') ? '?' : (format.match('S') ? '!' : null)); return show; }, /** Calculate the requested periods between now and the target time. @private @param inst {object} The current settings for this instance. @param show {string[]} Flags indicating which periods are requested/required. @param significant {number} The number of periods with values to show, zero for all. @param now {Date} The current date and time. @return {number[]} The current time periods (always positive) by year, month, week, day, hour, minute, second. */ _calculatePeriods: function(inst, show, significant, now) { // Find endpoints inst._now = now; inst._now.setMilliseconds(0); var until = new Date(inst._now.getTime()); if (inst._since) { if (now.getTime() < inst._since.getTime()) { inst._now = now = until; } else { now = inst._since; } } else { until.setTime(inst._until.getTime()); if (now.getTime() > inst._until.getTime()) { inst._now = now = until; } } // Calculate differences by period var periods = [0, 0, 0, 0, 0, 0, 0]; if (show[Y] || show[O]) { // Treat end of months as the same var lastNow = this._getDaysInMonth(now.getFullYear(), now.getMonth()); var lastUntil = this._getDaysInMonth(until.getFullYear(), until.getMonth()); var sameDay = (until.getDate() == now.getDate() || (until.getDate() >= Math.min(lastNow, lastUntil) && now.getDate() >= Math.min(lastNow, lastUntil))); var getSecs = function(date) { return (date.getHours() * 60 + date.getMinutes()) * 60 + date.getSeconds(); }; var months = Math.max(0, (until.getFullYear() - now.getFullYear()) * 12 + until.getMonth() - now.getMonth() + ((until.getDate() < now.getDate() && !sameDay) || (sameDay && getSecs(until) < getSecs(now)) ? -1 : 0)); periods[Y] = (show[Y] ? Math.floor(months / 12) : 0); periods[O] = (show[O] ? months - periods[Y] * 12 : 0); // Adjust for months difference and end of month if necessary now = new Date(now.getTime()); var wasLastDay = (now.getDate() == lastNow); var lastDay = this._getDaysInMonth(now.getFullYear() + periods[Y], now.getMonth() + periods[O]); if (now.getDate() > lastDay) { now.setDate(lastDay); } now.setFullYear(now.getFullYear() + periods[Y]); now.setMonth(now.getMonth() + periods[O]); if (wasLastDay) { now.setDate(lastDay); } } var diff = Math.floor((until.getTime() - now.getTime()) / 1000); var extractPeriod = function(period, numSecs) { periods[period] = (show[period] ? Math.floor(diff / numSecs) : 0); diff -= periods[period] * numSecs; }; extractPeriod(W, 604800); extractPeriod(D, 86400); extractPeriod(H, 3600); extractPeriod(M, 60); extractPeriod(S, 1); if (diff > 0 && !inst._since) { // Round up if left overs var multiplier = [1, 12, 4.3482, 7, 24, 60, 60]; var lastShown = S; var max = 1; for (var period = S; period >= Y; period--) { if (show[period]) { if (periods[lastShown] >= max) { periods[lastShown] = 0; diff = 1; } if (diff > 0) { periods[period]++; diff = 0; lastShown = period; max = 1; } } max *= multiplier[period]; } } if (significant) { // Zero out insignificant periods for (var period = Y; period <= S; period++) { if (significant && periods[period]) { significant--; } else if (!significant) { periods[period] = 0; } } } return periods; } }); })(jQuery); I have an online store that was created with PHP and it uses MySQL on the backend. There’s a boolean field in the database that either turns the store on (enabled) or it turns the store off (disabled) when it’s toggled. My question is can I write code that would automatically toggle that boolean value at specific times of the day? Hi Guys! Hoping someone can help me with this. I need to produce a timer which I can embed on a website. It needs to count down the remaining hours until 7pm during the weekdays, then at the weekends count down until 7pm on Monday. Any ideas or points in the right direction would be greatly appreciated! I'm a bit of a newbie! I have a loop creating a block_vars array for displaying records from a database. (In this case auction listings). The following code creates this array: // get random selection of 6 items for home page display $query = "SELECT id, title, current_bid, pict_url, ends, num_bids, minimum_bid, bn_only, buy_now, subtitle, starts, ends, reserve_price FROM " . $DBPrefix . "auctions WHERE closed = 0 AND suspended = 0 AND starts <= " . $NOW . " ORDER BY RAND() DESC LIMIT 6"; $res = mysql_query($query); $system->check_mysql($res, $query, __LINE__, __FILE__); $showendtime = false; $has_ended = false; $k = 0; while($row = mysql_fetch_assoc($res)) { $ends = $row['ends']; $difference = $ends - time(); if ($difference > 0) { $ends_string = FormatTimeLeft($difference); } else { $ends_string = $MSG['911']; } $high_bid = ($row['num_bids'] == 0) ? $row['minimum_bid'] : $row['current_bid']; $high_bid = ($row['bn_only'] == 'y') ? $row['buy_now'] : $high_bid; //determine whether the auction has a Buy Now price, show if appropriate. if($row['buy_now'] == '0.0000') { $bnamount = ""; } else { $bnamount = "<a href='" . $system->SETTINGS['siteurl'] . "buy_now.php?id=" . $row['id'] ."'>$" . substr($row['buy_now'], 0, -2) . "<br />" . "Buy Now</a>"; } //determine if the auction has a reserve set and if it has been met. display appropriate symbol if($row['reserve_price'] == '0.0000' && $row['bn_only'] == 'n' && ($row['current_bid'] < $row['minimum_bid'])) { $flag = '<img src="images/noreserve.gif" alt="No Reserve">'; $flagdesc = " No Reserve!"; $bnonly = "Current Bid<br />"; } else { if($row['current_bid'] >= $row['reserve_price']) { $flag = '<img src="images/reservemet.gif" alt="Reserve Met">'; $flagdesc = " Reserve Met!"; $bnonly = "Current Bid<br />"; } else { $flag = '<img src="images/noflag.gif">'; $flagdesc = ""; $bnonly = "Current Bid<br />"; } } if($row['reserve_price'] == '0.0000' && $row['bn_only'] == 'y') { $flag = '<img src="images/noflag.gif">'; $flagdesc = ""; $bnonly = "Asking Price "; $bnamount = ""; } $template->assign_block_vars('randomitems', array( 'ID' => $row['id'], 'BID' => $bnonly . "$" . substr($high_bid, 0, -2), 'IMAGE' => (!empty($row['pict_url'])) ? 'getthumb.php?w=' . $system->SETTINGS['thumb_show'] . '&fromfile=' . $uploaded_path . $row['id'] . '/' . $row['pict_url'] : 'images/email_alerts/default_item_img.jpg', 'TITLE' => $row['title'], 'BUY_NOW' => $bnamount, 'SUBTITLE' => $row['subtitle'], 'TIMELEFT' => $ends_string, 'NUMBIDS' => $row['num_bids'], 'FLAG' => $flag, 'FLAGDESC' => $flagdesc )); $k++; } Pretty straightforward. You will see I have a TIMELEFT value which will show on my home page as no.days, no.hours, no.mins until it closes etc. Here is the html in the tpl file: Code: [Select] <!-- BEGIN randomitems --> <div style="float:left;display:block;width:99%;border-bottom:#CCCCCC 1px solid;padding:2px;"> <div style="display:block;"> <table width="100%"> <tr> <td align="center" style="vertical-align:middle;width:120px" > <a href="{SITEURL}item.php?id={randomitems.ID}"><img src="{randomitems.IMAGE}" alt="{randomitems.TITLE}" style="border: none;width:120px"></a> </td> <td align="left" width="40%"> <table style="width:100%; vertical-align:middle; margin:0 auto"> <tr> <td><a id="itemdesc" href="{SITEURL}item.php?id={randomitems.ID}">{randomitems.TITLE}</a><br /> {randomitems.SUBTITLE}</td> </tr> <tr> <td id="closes">Closes in:{randomitems.TIMELEFT}</td> </tr> <tr> <td>{randomitems.FLAG}{randomitems.FLAGDESC}</td> </tr> </table> </td> <td id="buynow" align="center" width="15%"> {randomitems.BUY_NOW} </td> <td id="currentbid" align="center" width="25%"> {randomitems.BID} </td> </tr> </table> </div> </div> <!-- END randomitems --> This works quite nicely for me. What I want, and cannot manage thus far is to have the TIMELEFT value count down. The closest I have come is using a function like this: Code: [Select] <script type="text/javascript"> $(document).ready(function() { var currenttime = '{randomitems.TIMELEFT}'; function padlength(what) { var output=(what.toString().length == 1)? '0' + what : what; return output; } function displaytime() { currenttime -= 1; if (currenttime > 0){ var hours = Math.floor(currenttime / 3600); var mins = Math.floor((currenttime - (hours * 3600)) / 60); var secs = Math.floor(currenttime - (hours * 3600) - (mins * 60)); var timestring = padlength(hours) + ':' + padlength(mins) + ':' + padlength(secs); $("#ending_counter").html(timestring); setTimeout(displaytime, 1000); } else { $("#ending_counter").html('<span class="errfont">{L_911}</span>'); } } setTimeout(displaytime, 1000); }); </script> This code is placed in the tpl file and I simply give an html element the appropriate ID. The problem with this is that it would only show a countdown timer for the first item returned, which is no good as there are always six items returned. Can anyone offer a suggestion on how I could go about this? I can PM anyone with suggestions with a link to my development site so you know what I am raving about. Newbie PHP user here, so go easy on me . I've been trying to make a timer that starts on an arbitrary number of seconds, say 10 for example, then counts down to 0 (and refreshes itself every second for the user to see how much time is remaining), and then some more code is executed when 0 is reached. I attempted to do something like: <?php $goal = time() + 10; while($goal - time() >= 0) { echo "$goal - time()"; sleep(1); } ?> This didn't seem to work as my page just took a while to load - I suspect it was going through the entire while loop before displaying anything rather than just dynamically updating the timer every second. Also when I chose 100 instead of 10, the webpage didn't seem to even finish loading. Also it displayed a lot of odd text - not a timer at all! Help would be much appreciated ! |