Jump to content

[SOLVED] Invalid date format countdown script


DirtySnipe

Recommended Posts

Im trying to take a date format from a mysql table and use it as a countdown date. But I havnt got a cluse on what to edit in the javascript.

 

/*
Script: DHTML Easy Countdown (Development Version - not intended for production use due to uncompressed size)
Description: Counts down or up from a date (this is the non-XHTML version)
Author: Andrew Urquhart
Home: http://andrewu.co.uk/clj/countdown/
History:
20040117 0035UTC	v1		Andrew Urquhart		Created
20040119 1943UTC	v1.1	"					Made more accessible/easier to use
20040317 1548UTC	v1.2	"					Implemented a less intrusive error message
20040331 1408BST	v1.3	"					Attempts to append to the current onload schedule, rather than overriding it
20050210 1558GMT	v1.4	"					Saves a short-cut to each countdown Id element and re-uses that rather than continually calling getElementById - faster. Also reduced all nouns to short-versions to decrease size of script
20050216 0018GMT	v1.41	"					Added support for IE4
20051231 0210GMT	v1.5	"					Switched to custom date format and custom date parser for better internationalisation and cross-browser compatibility (should fix reports of recent bugs in Mac browsers)
20060101 1649GMT	v1.52	"					Made counter update 0.1s after the last whole second to ensure that the browser doesn't render the display at the exact moment the counter changes - otherwise can appear to miss a second out
20070414 2313BST	v1.53	"					Added 'typeof CD_T' check to CD_T() and test for CD_OBJS[id] in CD_D incase references don't exist when the timeout resolves
*/

// Tick (countdownId, eventDate)
function CD_T(id, e) {
var n = new Date();
CD_D(+n, id, e);
setTimeout("if(typeof CD_T=='function'){CD_T('" + id + "'," + e + ")}", 1100-n.getMilliseconds()); // We offset from 1100 so that our clock ticks every second (the millisecond correction each loop sees to that), but updates 0.1s after every whole second so that we don't accidentally read the same Date() twice in the same second
};

// Calculate time and update display (dateNow, countdownId, eventDate)
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);

// If you want to manually customise the counter display, then edit this line:
if (CD_OBJS[id]) {
	CD_OBJS[id].innerHTML = d + " day" + (d == 1 ? " " : "s ") + CD_ZP(h) + "h " + CD_ZP(m) + "m " + CD_ZP(s) + "s";
}
};

// Prefix single integers with a zero
function CD_ZP(i) {
return (i<10 ? "0" + i : i);
};

// Initialisation
function CD_Init() {
var pref = "countdown";
var objH = 1; // temp boolean true value
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; // Store global reference to countdown element object
				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>";
			}
		}
	}
}
};

// Get Date() object from 2006-01-01 00:00:00 GMT+00:00 date format
function CD_Parse(strDate) {
// Pattern match to a countdown date
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)) {
	// Start with a default date and build it up into the countdown date through Date setter methods
	var d = new Date(0);

	d.setUTCFullYear(+RegExp.$1,+RegExp.$2-1,+RegExp.$3); // Set YYYY-MM-DD directly as UTC
	d.setUTCHours(+RegExp.$4,+RegExp.$5,+RegExp.$6); // Set HH:MM:SS directly as UTC

	// If there is a timezone offset specified then we need to compensate for the offset from UTC
	var tzs	= (RegExp.$7 == "-" ? -1 : 1); // Timezone sign
	var tzh = +RegExp.$8; // Get requested timezone offset HH (offset ahead of UTC - may be negative)
	var tzm = +RegExp.$9; // Get requested timezone offset MM (offset ahead of UTC - always positive)
	if (tzh) {
		d.setUTCHours(d.getUTCHours() - tzh*tzs); // Compensate for timezone HH offset from UTC
	}
	if (tzm) {
		d.setUTCMinutes(d.getUTCMinutes() - tzm*tzs); // Compensate for timezone MM offset, depending on whether the requested MM offset is ahead or behind of UTC
	}
	return d; // Date now correctly parsed into a Date object correctly offset from UTC internally regardless of users current timezone.
}
else {
	return NaN; // Didn't match required date format
};
};

var CD_OBJS = new Object();

// Try not to commandeer the default onload handler if possible
if (window.attachEvent) {
window.attachEvent('onload', CD_Init);
}
else if (window.addEventListener) {
window.addEventListener("load", CD_Init, false);
}
else {
window.onload = CD_Init;
}

 

Im trying to use the following variable:-

 

<span id="countdown1"><? echo $endProductionTime;</span>!

 

Can somone help me?

 

 

The date format it should be in is 'YYYY-MM-DD HH:MM:SS'

 

Link to comment
Share on other sites

You shouldn't need to edit the JavaScript, just output the database date in the correct format. Not sure from your post if you are using the text within the SPAN above to set the countdown date. If you are, this should work based upon what I see the Javascript functions expecting

 

<span id="countdown1"><? echo echo date('Y-m-d H:i:s \G\M\TO', $dateFromDB);</span>!

 

Output will be like this:

2009-05-26 15:05:46 GMT-0500

Link to comment
Share on other sites

I have just noticed that this code is not counting down??? its counting up which is very odd...

 

I used the following

 

<span id="countdown1"><? echo date('Y-m-d H:i:s \G\M\TO',  $endProductionTime); ?> </span>

 

 

$endProductionTime = 2009-06-27 04:55:00

 

But the countdown is displaying  = 14391 days 09h 33m 24s and is counting up...

 

 

 

Link to comment
Share on other sites

The script counts down or up based upon the date you pass it. If it is counting up then the date you are setting is in the past. Look at the rendered HTML to see what is being written to the page.

 

Is there any way to set a start date in the same format using a variable?

 

What EXACTLY are you wanting to do? Are you just wanting to displya a different start date on the page or are you wanting the countdown to start at a different date/time If it is the latter, EXACTLY how are you wanting that to work. For example, if the start date was set to June 1st and the end date is June 30th, does the page not show any countdown activity if accessed prior to June 1st? Need more info. Also, if there is anything else you aere going to request on this post do it now.

Link to comment
Share on other sites

I have two variables

 

$beginProductionTime

 

and

 

$endProductionTime

 

Both are set to type datetime

 

What I am trying to do is display all the records in the database in a table format and I want to show an active countdown for each record using the start date ($beginProductionTime) and the finish date ($endProductionTime)

 

I have a very basic output of the information into a table format but i cannot get the countdown to work correctly.

 

 

I have looked at the html output and it is not displaying the date correctly. For some reason it is changing the result to <span id="countdown1">1970-01-01 01:33:29 GMT+0100 </span>.

 

But in the database it is 2009-06-27 04:55:00

Link to comment
Share on other sites

First you need to resolve the problem of the date being written correctly. Have you verified that you are returning the value correctly from the database? I expect not based upoon your results. Try this to make sure you are using a legitimate value:

 

<span id="countdown1"><? echo date('Y-m-d H:i:s \G\M\TO',  $endProductionTime); ?> </span>
<br />Raw Date/Time from DB: <?php echo $endProductionTime; ?>

 

As per your second item of a start date you still disn't answer my questions. I'm not sure how the start date/time has any relevance on what the count down time would be (unless the start date hasn't occured yet.) Please prove EXPLICIT details of how you want the start date to function. I can see several different uses for a start date. Here are the possible scenarios.

 

Start Date & End Date are in the past: What would you want shown in this scenario? Maybe just the text "Complete"?

 

Start Date is in the Past & End Date is in the future: Do you want to show the count down time using the script from the current time? If so, what is the purpose of the start date? Can't see how it would be used in this scenario. I guess you could show how much time has elapsed from the start date along with the remaining time.

 

Start Date and End Date are in the future: What do you want to do in this scenario. Maybe just display the text "Not started"

 

You are not providing enough information to provide help on the use of a start date.

Link to comment
Share on other sites

Please forget about the start date I will go back to that at another time.

 

I have put your code in and it is still returning the same error.

 

 

14392 days 06h 55m 54s

Raw Date/Time from DB: 2009-06-27 04:55:00

 

It is still counting up?

Link to comment
Share on other sites

You know I just realized that I am needlessly converting the data twice - ending up with what I started!

 

It would make more sense to just echo the date from the DB and append the GMT part

 

<span id="countdown1"><? echo $endProductionTime . date(' \G\M\TO'); ?> </span>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.