Date comparison in Asterisk

Hi guys,

I want to do date comparison in Asterisk. I mean compare 2 dates. For example

If user_date >= asterisk server_date then the call should proceed.

I get the server date and store in a variable like this

same => n,Set(GLOBAL(server_date)=${STRFTIME(,%Y-%m-%d)}})

Then in my func_odbc.conf I have a function that get the date of the user

prefix = START
dsn = asterisk
readsql = SELECT date(user_date) from customers WHERE user_id =’${SQL_ESC(${ARG1})}’

So in the extensions.conf I want to check if the user_date is reached like then the call should proceed. I did it like this

same => n,Set(start_date=${START_DATE(${user_id})})
same => n,GotoIf($[${server_date} >= ${start_date}]?continue,1:availtocall,1)

In my head, the above statement will do 2017-01-31 >= 2017-02-02 which I am not sure it is correct for date comparison.

Convert both your dates to seconds since the epoch and do the math that way.


How do I do the conversion, remember the variable holds the value 2017-01-31. How do I convert it to seconds?

Use one of the scripting solutions. Although this might just be within the capabilities of shelling out to /bin/date.

I think ${EPOCH} get the Asterisk server time in seconds but how to I convert the other date to seconds so I can do the math.

Ohh I think I just need to convert the other date to seconds using SQL query. Let me try that and see

1 Like

You should be able to use the UNIX_TIMESTAMP function of your SQL server

select UNIX_TIMESTAMP(user_date) from customers WHERE user_id ='${SQL_ESC(${ARG1})}'

1 Like

Thank you very much it worked.