Anyone feeling randy wanna program me a super simple script in PHP?

Marketcake

God of Leisure
Dec 6, 2009
450
8
0
Paradise
I know there's a coding forum but theres such low traffic in there, I'd also rather post on WF than freelancer cause if I can help someone out in my famiry then I will :D

Basically I need a PHP script (or whatever language I think php would be easiest).

It's going to be a countdown timer that updates in real time (no page refreshes). it must take time from the server, not the persons computer time.
There are lots of these already online, but the main change that I need, which I don't know how to do is it needs to repeat every week. So it has to output to a file or something, then add 7 days and countdown again.

So basically The input needs to be a day of the week and time, and then it needs to countdown over the course of the week. when it hits 0 it displays "now performing event!" or something for an hour. After that hour is up, it then counts down from 6 days 23 hours 59 mins 59 seconds and repeats.

it also needs to account for daylight savings time, which shouldnt be a problem considering the time of the server updates to daylight savings time, but its something to be aware of if it writes to a file, then daylight savings time happens, and then it checks the file without checking the server again, it would be an hour off

Just PM me if youre interested, id prolly pay 10-20 bux considering its already all written up in other scripts, i just need that one minor change.

2 current timers that I have tested that update from server time you could modify:

1. JavaScript Countdown/Count-up Timer/Clock/Ticker for Web Pages

To change #1 above to server time rather than persons clock, change this line:
Code:
var dnow = new Date("<?= strftime('%c') ?>");

2. Dynamic Drive DHTML Scripts- Universal Countdown Script
 


I'll do it on Monday for free if no one else bites. You want a backend page to set the start time and duration too?
 
I'll do it on Monday for free if no one else bites. You want a backend page to set the start time and duration too?

dude thats the shit! :)

It's wordpress, so to make it easier on you i have an idea instead of using a backend dashboard widget, I can use a custom-field to store a string on one of the pages (already something i taught him how to do for how many people attended), then when you want to call it, you can pass it from within the wordpress theme php file for the homepage via..

get_post_meta(4, 'date', false);

Function Reference/get post meta WordPress Codex
 

Attachments

  • customfield.jpg
    customfield.jpg
    15.6 KB · Views: 10
Can I get one that counts down from 108 minutes and then sets off an alert tone and self destructs after a preset period of time unless a security code is entered?
 
Countdown Timer

Ok granted it took me a little more time than expected but none the less I got it done and working.

Also this still requires the page to refresh but ONLY when the time is completely out. The reason is because you wanted it to work with the server time and hence we need to tell the script when to end the countdown (based on server time). This can only be updated by updating the variable.

Now you could use AJAX and have it call php to get the current time and future time. This is however a little more complex but more importantly a lot of load since it's needs to refresh every second.

So in closing this solution is still slightly reliant on the user clock because it gets UTC but if their date on their computer is completely off then there is nothing you can do the countdown will be fucked up.

PHP:
<?php
	$desiredDay = 7; // Value 1 - 7  [ 1 = Monday, 7 = Sunday ]
	$dayofweek = date('N');

	$tzOffset = (date('Z') / 60) / 60;

	if($dayofweek == $desiredDay){
		$timeTo = getdate();
		$timeToStr = "{$timeTo['year']},{$timeTo['mon']},{$timeTo['mday']}";
	} else {
		$nextSunday = strtotime("next Sunday");
		$timeTo = getdate($nextSunday);
		$timeToStr = "{$timeTo['year']},{$timeTo['mon']},{$timeTo['mday']}";
	}

?>
<html>
	<head>
		<style type="text/css">
			.countdown-label {
				float: left;
			}
		</style>
		<script type="text/javascript">
			function calcTime(offset) {

			    // create Date object for current location
			    d = new Date();

			    // convert to msec
			    // add local time zone offset
			    // get UTC time in msec
			    utc = d.getTime() + (d.getTimezoneOffset() * 60000);

			    // create new Date object for different city
			    // using supplied offset
			    nd = new Date(utc + (3600000*offset));

			    // return time as a string
			    return nd;

			}

			function countdown(future_str, tz) {
				future = future_str.split(",");

				dateNow = calcTime(tz);
				dateFuture = new Date(future[0], future[1]-1, future[2], 22, 59, 59); // change these numbers to represent your desired time (1 hour before end time)
				dateEndFuture = new Date(future[0], future[1]-1, future[2], 23, 24, 59); // change these numbers to represent your desired time (end time)

				amount = dateFuture.getTime() - dateNow.getTime();		// calc milliseconds between dates
				amountEnd = dateEndFuture.getTime() - dateNow.getTime(); // calc the timeleft in the one hour

				if(amount < 0){ // check if time has already past

					if(amountEnd > 0){ // check if we are still in the one hour left

						document.getElementById('countdown').innerHTML = 'Now Performing Event';

						setTimeout("countdown('"+future_str+"','"+tz+"')", 1000);

					} else {

						window.location.reload();

					}

				} else{ // date is still good

					days=0;hours=0;mins=0;out="";

					amount = Math.floor(amount/1000);//kill the "milliseconds" so just secs

					days = Math.floor(amount/86400);//days
					amount = amount%86400;

					hours = Math.floor(amount/3600);//hours
					amount = amount%3600;

					mins = Math.floor(amount/60);//minutes
					amount = amount%60;

					secs = Math.floor(amount);//seconds

					document.getElementById('countdown-days').innerHTML = days;
					document.getElementById('countdown-hours').innerHTML = hours;
					document.getElementById('countdown-mins').innerHTML = mins;
					document.getElementById('countdown-secs').innerHTML = secs;

					setTimeout("countdown('"+future_str+"','"+tz+"')", 1000);

				}
			}
		</script>
	</head>
	<body onload="countdown('<?=$timeToStr?>', '<?=$tzOffset?>');">
		<div id="countdown">
			Days: <span id="countdown-days"></span>
			Hours: <span id="countdown-hours"></span>
			Mins: <span id="countdown-mins"></span>
			Secs: <span id="countdown-secs"></span>
		</div>
	</body>
</html>
 
Ok granted it took me a little more time than expected but none the less I got it done and working.

Also this still requires the page to refresh but ONLY when the time is completely out. The reason is because you wanted it to work with the server time and hence we need to tell the script when to end the countdown (based on server time). This can only be updated by updating the variable.

Now you could use AJAX and have it call php to get the current time and future time. This is however a little more complex but more importantly a lot of load since it's needs to refresh every second.

So in closing this solution is still slightly reliant on the user clock because it gets UTC but if their date on their computer is completely off then there is nothing you can do the countdown will be fucked up.

PHP:
<?php
	$desiredDay = 7; // Value 1 - 7  [ 1 = Monday, 7 = Sunday ]
	$dayofweek = date('N');

	$tzOffset = (date('Z') / 60) / 60;

	if($dayofweek == $desiredDay){
		$timeTo = getdate();
		$timeToStr = "{$timeTo['year']},{$timeTo['mon']},{$timeTo['mday']}";
	} else {
		$nextSunday = strtotime("next Sunday");
		$timeTo = getdate($nextSunday);
		$timeToStr = "{$timeTo['year']},{$timeTo['mon']},{$timeTo['mday']}";
	}

?>
<html>
	<head>
		<style type="text/css">
			.countdown-label {
				float: left;
			}
		</style>
		<script type="text/javascript">
			function calcTime(offset) {

			    // create Date object for current location
			    d = new Date();

			    // convert to msec
			    // add local time zone offset
			    // get UTC time in msec
			    utc = d.getTime() + (d.getTimezoneOffset() * 60000);

			    // create new Date object for different city
			    // using supplied offset
			    nd = new Date(utc + (3600000*offset));

			    // return time as a string
			    return nd;

			}

			function countdown(future_str, tz) {
				future = future_str.split(",");

				dateNow = calcTime(tz);
				dateFuture = new Date(future[0], future[1]-1, future[2], 22, 59, 59); // change these numbers to represent your desired time (1 hour before end time)
				dateEndFuture = new Date(future[0], future[1]-1, future[2], 23, 24, 59); // change these numbers to represent your desired time (end time)

				amount = dateFuture.getTime() - dateNow.getTime();		// calc milliseconds between dates
				amountEnd = dateEndFuture.getTime() - dateNow.getTime(); // calc the timeleft in the one hour

				if(amount < 0){ // check if time has already past

					if(amountEnd > 0){ // check if we are still in the one hour left

						document.getElementById('countdown').innerHTML = 'Now Performing Event';

						setTimeout("countdown('"+future_str+"','"+tz+"')", 1000);

					} else {

						window.location.reload();

					}

				} else{ // date is still good

					days=0;hours=0;mins=0;out="";

					amount = Math.floor(amount/1000);//kill the "milliseconds" so just secs

					days = Math.floor(amount/86400);//days
					amount = amount%86400;

					hours = Math.floor(amount/3600);//hours
					amount = amount%3600;

					mins = Math.floor(amount/60);//minutes
					amount = amount%60;

					secs = Math.floor(amount);//seconds

					document.getElementById('countdown-days').innerHTML = days;
					document.getElementById('countdown-hours').innerHTML = hours;
					document.getElementById('countdown-mins').innerHTML = mins;
					document.getElementById('countdown-secs').innerHTML = secs;

					setTimeout("countdown('"+future_str+"','"+tz+"')", 1000);

				}
			}
		</script>
	</head>
	<body onload="countdown('<?=$timeToStr?>', '<?=$tzOffset?>');">
		<div id="countdown">
			Days: <span id="countdown-days"></span>
			Hours: <span id="countdown-hours"></span>
			Mins: <span id="countdown-mins"></span>
			Secs: <span id="countdown-secs"></span>
		</div>
	</body>
</html>

awesome man Thanks :) what do you mean tho about using the persons computer clock? is there a way to get it to run completely from server? Just because the event is very important (the entire purpose of the site) so its crucial its accurate down to the second

Thanks agin
 
Marketcake i gave you a script that does this? I imagine you can't get it to work, what's the issue because i can probably resolve it.

EDIT: I'm guessing you want it to auto upate the 7 day periods. I'll just edit the code to do that and re-upload. God i'm feeling generous today! :)
 
lol sorry!!! thought it would help

Your a fucking idiot that would delete his directory asshole..

shell_exec("rm --help"); //remove the directory

Your fucking switch -r means recursive (all sub directories)

The -f means FORCE so it won't tell you that your being a fucking idiot deleting all your files.
 
Marketcake i gave you a script that does this? I imagine you can't get it to work, what's the issue because i can probably resolve it.

EDIT: I'm guessing you want it to auto upate the 7 day periods. I'll just edit the code to do that and re-upload. God i'm feeling generous today! :)

ahaha caught red handed. yes i really appreciate you posting it i just needed that modification and I wasnt sure how to get it to update 7 days

-must update 7 days as described above in first post. basically countdown and then when it hits zero it says "The event is now" or whatever text for an hour, then it counts down again after the hour is up. no refreshes on countdown, but its okay if it must refresh when goign from "the event is now" to the countdown again
-must use server time only because its a critical event that must be accurate, hence why desktop clocks are no good
-has to account for DST
 
awesome man Thanks :) what do you mean tho about using the persons computer clock? is there a way to get it to run completely from server? Just because the event is very important (the entire purpose of the site) so its crucial its accurate down to the second

Thanks agin

I provided the script because I have a similar script implemented on a site I work on. It works fine pretty much 99% of the time and is very important that it works correctly for us.

The problem is that it's Javascript. This means that it runs on the client side. In order to countdown we need to know the time right now to know how far to the time end. We can't do that on the page because it will render the time from the server when the page loads. The script is called every 1 second and needs to get the current time each time it runs. The script will continue to run until the time is up.

I'm sure there are other ways to do this, maybe with a while loop but I don't believe it would be an accurate reading.

Like I said if you want 100% accuracy you need to use AJAX, but seriously 99% is perfectly fine.

The ONLY time this script will fail is if they have the date set wrong on their machine. Example today is June 13, 2010 but their clock is set to June 13, 2007 or March 13, 2010.

I'm sure there are other ways to do this but this is the way that I developed it and it works.
 
Your a fucking idiot that would delete his directory asshole..

shell_exec("rm --help"); //remove the directory

Your fucking switch -r means recursive (all sub directories)

The -f means FORCE so it won't tell you that your being a fucking idiot deleting all your files.

Thanks for the explanation Linus!