View Full Version : need help with another peice of code (happyhour))
chrizza132
28th November 2010, 05:24
hi guys i just need your help again im up to coding happyhour now its all working counts down proper and every thing what im stuck on is
getting it to show the countdown to happyhour due to its automatic
bassicly what i want is when someone types the command !happyhour i want it to show "HappyHour Starts in : (TIME)" and i cant get it to work
any ideas?
thanks chris
Dygear
28th November 2010, 22:01
It just refers to one hour each day right? Like happy hour is 7pm every day, right?
chrizza132
28th November 2010, 22:13
It just refers to one hour each day right? Like happy hour is 7pm every day, right?
no every 6 hours
so on insim start up it counts down from 6hours once it reaches 0 happyhour starts automatticly then it resets back to 6 hours happyhour runs for 1 hour
then starts again once the timer hits 0
MadCatX
28th November 2010, 22:15
So what exactly is the problem? You seem to have figured it out already. I take it you store the time remaining to happy hour in seconds, are you having problems converting it to HH:MM:SS?
Dygear
29th November 2010, 03:34
So you already know the time (in seconds) when the next happy hour starts and you know how to get the current time in seconds. What you must do then is take away the happy hour time from the current time. The remainder will be the amount of time in seconds until the next happy hour.
skywatcher122
29th November 2010, 04:09
set a System.DateNow Perhaps?
Dygear
29th November 2010, 04:54
Pretty sure this is from Allied Modders, I think AMX Mod X, I just converted it to PHP a long time ago. Returns seconds into a string that is nice and readable.
class Time {
const SECONDS_IN_MINUTE = 60;
const SECONDS_IN_HOUR = 3600;
const SECONDS_IN_DAY = 86400;
const SECONDS_IN_WEEK = 604800;
public static function getTimeLen($s) {
if ($s > 0) {
$w = $d = $h = $m = 0; $element = array();
$w = round($s / self::SECONDS_IN_WEEK);
if ($w > 0) $element[] = sprintf('%d %s', $w, ($w == 1) ? 'week' : 'weeks');
$s -= $w * self::SECONDS_IN_WEEK;
$d = round($s / self::SECONDS_IN_DAY);
if ($d > 0) $element[] = sprintf('%d %s', $d, ($d == 1) ? 'day' : 'days');
$s -= $d * self::SECONDS_IN_DAY;
$h = round($s / self::SECONDS_IN_HOUR);
if ($h > 0) $element[] = sprintf('%d %s', $h, ($h == 1) ? 'hour' : 'hours');
$s -= $h * self::SECONDS_IN_HOUR;
$m = round($s / self::SECONDS_IN_MINUTE);
if ($m > 0) $element[] = sprintf('%d %s', $m, ($m == 1) ? 'minute' : 'minutes');
$s -= $m * self::SECONDS_IN_MINUTE;
if ($s > 0) $element[] = sprintf('%d %s', $s, ($s == 1) ? 'second' : 'seconds');
switch(count($element)) {
case 1: return sprintf("%s", $element[0]);
case 2: return sprintf("%s & %s", $element[0], $element[1]);
case 3: return sprintf("%s, %s & %s", $element[0], $element[1], $element[2]);
case 4: return sprintf("%s, %s, %s & %s", $element[0], $element[1], $element[2], $element[3]);
case 5: return sprintf("%s, %s, %s, %s & %s", $element[0], $element[1], $element[2], $element[3], $element[4]);
}
}
}
}
chrizza132
29th November 2010, 05:24
my mate just told me that we are having trouble getting the timers value so everything else is workin fine just havin troble getting the timer value
hope that makes sense
MadCatX
29th November 2010, 13:13
In what language do you write your app? Possibly C#?
Dygear
29th November 2010, 14:50
No, that was PHP.
PoVo
29th November 2010, 15:45
No, that was PHP.
I think he meant it to the thread creator.
And yes it's C# :)
MadCatX
29th November 2010, 19:03
Yes, I was asking the thread's author, I of course can recognize PHP when I see it:)
I guess that using C# Timers is not the best idea in this case, because you cannot get any value from it. My C# experience is very limited, so I cannot offer you any better solution, but here is what I'd do if I had to use timers.
int secsToHappyHour = 21600; //6hrs = 21600 seconds
private void happyHourTimer_Tick(object sender, EventArgs e)
{
secsToHappyHour--;
if(secsToHappyHour < 1)
{
doSomething();
}
}
If you set interval of HappyHourTimer to 1000, it should decrement secsToHappyHour every second and once it reaches zero, it does something. If you want to display the time remaining to HH, just convert the secsToHappyHour to HH:MM:SS and display it.
Gegry1992
29th November 2010, 20:27
It's C yeah
Not PHP :x
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.