Raised This Month: $12 Target: $400
 3% 

/time countdown


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
Nutu_
AlliedModders Donor
Join Date: Mar 2016
Location: Germany
Old 10-28-2022 , 20:31   /time countdown
Reply With Quote #1

Is there any way for someone to create a plugin where if someone says /countdown to show how exactly (years)/months/days/minutes/seconds until a chosen day are?
__________________
a simple act of caring creates an endless ripple.
Nutu_ is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 10-28-2022 , 22:36   Re: /time countdown
Reply With Quote #2

Here is a quick version. Note that the days might be a bit off and the month in some rare cases could be one off due to having to assume each month is only 30 days. Doing it correctly I think it more complicated.

The countdown_target cvar needs to the the unix timestamp of the target date/time. You can convert a date/time to a unix timestamp on several websites online.

PHP Code:
#include <amxmodx>

new g_pCvarTargetTime

public plugin_init()
{
    
register_plugin("Countdown Time Display""0.1""Me");
    
register_clcmd("say /countdown""cmdCountdown");
    
g_pCvarTargetTime register_cvar("countdown_target""1671954140")
}

public 
cmdCountdown(id)
{
    new 
iSecondsiMinutesiHoursiDaysiMonthsiYears;
    new 
szMessage[128], len;
    
    
iSeconds get_pcvar_num(g_pCvarTargetTime) - get_systime();

    if( 
iSeconds )
    {
        
iYears iSeconds 31536000
        iMonths 
iSeconds 31536000 2592000;
        
iDays iSeconds 2592000 86400;
        
iHours iSeconds 86400 3600;
        
iMinutes iSeconds 3600 60;
        
iSeconds iSeconds 60;

        if( 
iYears )
        {
            
len += formatex(szMessage[len], charsmax(szMessage) - len"%dy "iYears)
        }

        if( 
iMonths )
        {
            
len += formatex(szMessage[len], charsmax(szMessage) - len"%dm "iMonths)
        }
        
        if( 
iDays )
        {
            
len += formatex(szMessage[len], charsmax(szMessage) - len"%dd "iDays)
        }
        
        if( 
iHours )
        {
            
len += formatex(szMessage[len], charsmax(szMessage) - len"%dh "iHours)
        }
        
        if( 
iMinutes )
        {
            
len += formatex(szMessage[len], charsmax(szMessage) - len"%dm "iMinutes)
        }
        
        if( 
iSeconds )
        {
            
len += formatex(szMessage[len], charsmax(szMessage) - len"%ds"iSeconds)
        }

        
client_print(idprint_chatszMessage);
    }
    else
    {
        
client_print(idprint_chat"The countdown has expired.");
    }

    return 
PLUGIN_HANDLED

__________________

Last edited by fysiks; 10-28-2022 at 22:40.
fysiks is offline
Nutu_
AlliedModders Donor
Join Date: Mar 2016
Location: Germany
Old 10-29-2022 , 07:46   Re: /time countdown
Reply With Quote #3

It works! Thank you very much!
__________________
a simple act of caring creates an endless ripple.
Nutu_ is offline
iceeedr
Veteran Member
Join Date: Apr 2017
Location: Brazil
Old 10-29-2022 , 10:26   Re: /time countdown
Reply With Quote #4

I think that using parse_time we were able to overcome the problem of months with plus or minus days.

PHP Code:
#include <amxmodx>

#define PLUGIN  "AlliedTest"
#define VERSION "1.0"
#define AUTHOR  "iceeedR"

new g_CvarDate[30]

public 
plugin_init()
{
        
register_plugin(PLUGINVERSIONAUTHOR)

        
bind_pcvar_string(create_cvar("amx_target_time""09:00:00 11/22/2023"), g_CvarDatecharsmax(g_CvarDate))
}

public 
plugin_cfg() 
        
CalculateStrings()

public 
CalculateStrings()
{
        new 
iTargetDateTime parse_timeg_CvarDate "%H:%M:%S %m/%d/%Y" )
        new 
iCurrentDateTime get_systime()

        new 
Counter iTargetDateTime iCurrentDateTime
        
        
new iSeconds iMinutes iHours iDays

        convert_seconds
Counter iSeconds iMinutes iHours iDays );

        if(
Counter 0)
        {
                if(
iDays)
                {
                        
server_print("Time to day %s: %d day(s) and %d Hour(s)"g_CvarDateiDaysiHours)
                        return 
PLUGIN_HANDLED
                
}

                
server_print("Time to day %s: %d:%d:%d"g_CvarDateiHoursiMinutesiSeconds)
                return 
PLUGIN_HANDLED
        
}
        else
        {
                
server_print("Has passed: %d day(s) %d Hour(s) %d Minute(s) and %d Second(s) until %s"iDays *= -1iHoursiMinutesiSecondsg_CvarDate)
                return 
PLUGIN_HANDLED
        
}
}

convert_secondsiTotalSeconds , &seconds , &minutes , &hours , &days )
{
        
days iTotalSeconds 86400
        hours 
= ( iTotalSeconds 3600 ) % 24
        minutes 
= ( iTotalSeconds 60 ) % 60
        seconds 
iTotalSeconds 60

It is also possible to get the time elapsed from the date.

HTML Code:
Time to day 09:00:00 11/22/2023: 388 day(s) e 19 Hour(s)
Has passed: 4 day(s) 3 Hour(s) 49 Minute(s) and 46 Second(s) until 18:00:00 10/25/2022
__________________


Quote:
Originally Posted by fysiks View Post
Please stop trying to help. You appear to just be posting random stuff. Wait until you actually understand more about AMX Mod X and how the game works.
https://iceeedr.com.br/

Last edited by iceeedr; 10-29-2022 at 13:11.
iceeedr is offline
Send a message via Skype™ to iceeedr
Nutu_
AlliedModders Donor
Join Date: Mar 2016
Location: Germany
Old 10-29-2022 , 11:41   Re: /time countdown
Reply With Quote #5

i just noticed that fysiks plugin shows now that countdown expired even if it doesn't and it showed at first exactly the time i specified, i tried to change the cvar to another date, but it still shows me that the coundown expired
i will try icedr's one and come with a feedback!
__________________
a simple act of caring creates an endless ripple.
Nutu_ is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 10-29-2022 , 14:44   Re: /time countdown
Reply With Quote #6

Quote:
Originally Posted by iceeedr View Post
I think that using parse_time we were able to overcome the problem of months with plus or minus days.
I thought about going that route but I don't use parse_time() often and it was late and didn't feel like doing it last night as it was a long day.

Quote:
Originally Posted by Nutu_ View Post
i just noticed that fysiks plugin shows now that countdown expired even if it doesn't and it showed at first exactly the time i specified, i tried to change the cvar to another date, but it still shows me that the coundown expired
i will try icedr's one and come with a feedback!
I'm not sure how that is possible. If the timstamp in the cvar is at a future date it can't show that it has expired unless your date is outside the range of a signed 32-bit integer. What value are you using for the cvar?
__________________
fysiks is offline
EFFx
Veteran Member
Join Date: Feb 2016
Location: São Paulo, Brasil
Old 10-29-2022 , 15:57   Re: /time countdown
Reply With Quote #7

Took it off my VIP Expire plugin, just removed some parts:

PHP Code:
#include <amxmodx>
#include <amxmisc>

#define PLUGIN "New Plug-In"
#define VERSION "1.0"
#define AUTHOR "author"

#define parseTime(%1)            parse_time(%1, "%m/%d/%Y %H:%M:%S")

const iYearSeconds =            31536000
const iMonthSeconds =             2629800
const DaySeconds =             86400
const HourSeconds =             3600
const MinuteSeconds =             60

public plugin_init() 
{
    
register_plugin(PLUGINVERSIONAUTHOR)
    
    
register_concmd("amx_display_time""cmdDateCommand"ADMIN_KICK"<date format> - example: 05/01/2025 18:00:00")
}

public 
cmdDateCommand(idiLeveliCid)
{
    if(!
cmd_access(idiLeveliCid2))
        return 
PLUGIN_HANDLED
        
    
new szTime[60]
    
read_argv(1szTimecharsmax(szTime))
    
    new 
szMessage[192], iTime parseTime(szTime)
    
formatTime(iTimeszMessagecharsmax(szMessage))
    
console_print(id"Time remaining: %s"szMessage)
    return 
PLUGIN_HANDLED
}

formatTime(iTimeszMessage[] = ""iLen 0

    if(!
iTime)
    {
        
formatex(szMessageiLen"No time to expire")
        return
    }
    
iTime -= get_systime()
    
    new 
iYeariMonthiDays
    
new iHouriMinuteiSecond
    iTime 
-= iYearSeconds * (iYear iTime iYearSeconds)
    
iTime -= iMonthSeconds * (iMonth iTime iMonthSeconds)
    
iTime -= DaySeconds * (iDays iTime DaySeconds)
    
iTime -= HourSeconds * (iHour iTime HourSeconds
    
iTime -= MinuteSeconds * (iMinute iTime MinuteSeconds
    
iSecond iTime

    
new szMessageFmt[64]
    if(
iYear)
    {
        
formatex(szMessageFmtiLen"%s%d year%s"szMessageFmtiYear, (iYear 1) ? "s" "")
    }

    if(
iMonth)
    {
        
formatex(szMessageFmtiLen"%s, %d month%s"szMessageFmtiMonth, (iMonth 1) ? "s" "")
    }

    if(
iDays)
    {
        
formatex(szMessageFmtiLen"%s%s %d day%s"szMessageFmtiHour "," " and"iDays, (iDays 1) ? "s" "")
    }
        
    if(
iHour)
    {
        
formatex(szMessageFmtiLen"%s%s %d hour%s"szMessageFmtiMinute "," " and"iHour, (iHour 1) ? "s" "")
    }
        
    if(
iMinute)
    {
        
formatex(szMessageFmtiLen"%s%s %d minute%s"szMessageFmtiSecond "," " and"iMinute, (iMinute 1) ? "s" "")
    }
        
    if(
iSecond)
    {
        
formatex(szMessageFmtiLen"%s and %d second%s"szMessageFmtiSecond, (iSecond 1) ? "s" "")
    }
    
copy(szMessageiLenszMessageFmt)

To use the command properly:

amx_display_time month/day/year
If you want to know by hour/minute/second, just pass them as hour:minute:second

Example: amx_display_time "10/30/2023"

Code:
Time remaining: 1 year and 1 day
Example: amx_display_time "6/30/2027"

Code:
Time remaining: 4 years, 8 months, 1 day and 12 hours
Example: amx_display_time "6/30/2027 18: 30:15"

Code:
Time remaining: 4 years, 8 months, 1 day, 13 hours, 24 minutes and 52 seconds
__________________
• Ranking System • AutoMix 5vs5 System
• Web Ban System • Plugins for free

____________________________________________
For private works:
• Discord: EFFEXo#8850 • Steam: EFFEXo

Last edited by EFFx; 10-29-2022 at 16:13.
EFFx is offline
fysiks
Veteran Member
Join Date: Sep 2007
Location: Flatland, USA
Old 10-29-2022 , 19:27   Re: /time countdown
Reply With Quote #8

@EFFx,
  • According to the formatex() documentation, using the output string as an input is not allowed. But, I'm assuming you're not getting any errors so in this one specific case, it may work. As far as I have seen, the proper way to append with formatex() is the way that I've done it in my plugin above.
  • You actually have a potential overflow error because you're using iLen which is an input to effectively index a hard coded array inside the function (i.e. szMessageFmt). Simply remove szMessageFmt entirely and use szMessage directly.
  • The way you calculate each of the time units requires more operations than the way that I do it though it's unlikely to cause any significant performance hit.
__________________
fysiks is offline
KrazyKat
Member
Join Date: Mar 2021
Old 10-29-2022 , 21:07   Re: /time countdown
Reply With Quote #9

Quote:
Originally Posted by EFFx View Post
Example: amx_display_time "6/30/2027 18: 30:15"

Code:
Time remaining: 4 years, 8 months, 1 day, 13 hours, 24 minutes and 52 seconds
https://www.youtube.com/watch?v=FbKNVKjIwMk
KrazyKat is offline
EFFx
Veteran Member
Join Date: Feb 2016
Location: São Paulo, Brasil
Old 10-29-2022 , 23:15   Re: /time countdown
Reply With Quote #10

I've been using this system in a 24/7 32/32 players server and no errors displayed with even more calculations and display formats until now.

Quote:
Originally Posted by KrazyKat View Post
Lol I just wrote a random date.
__________________
• Ranking System • AutoMix 5vs5 System
• Web Ban System • Plugins for free

____________________________________________
For private works:
• Discord: EFFEXo#8850 • Steam: EFFEXo

Last edited by EFFx; 10-29-2022 at 23:16.
EFFx is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 03:40.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode