# Date comparison in Asterisk

**URL:** <https://community.asterisk.org/t/date-comparison-in-asterisk/69582>\
**Category:** Asterisk Support\
**Created:** [January 31, 2017, 7:57pm UTC](https://community.asterisk.org/t/date-comparison-in-asterisk/69582 "2017-01-31T19:57:28Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![oladapoadebowale](https://sea1.discourse-cdn.com/asterisk/user_avatar/community.asterisk.org/oladapoadebowale/32/1813_2.png) [@oladapoadebowale](https://community.asterisk.org/u/oladapoadebowale)\
**Post date:** [January 31, 2017, 7:57pm UTC](https://community.asterisk.org/t/date-comparison-in-asterisk/69582/1 "2017-01-31T19:57:28Z")

</div>

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

[DATE]  
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.

---

<div class="post-metadata">

**Author:** ![johnkiniston](https://sea1.discourse-cdn.com/asterisk/user_avatar/community.asterisk.org/johnkiniston/32/28_2.png) [@johnkiniston](https://community.asterisk.org/u/johnkiniston)\
**Post date:** [January 31, 2017, 10:40pm UTC](https://community.asterisk.org/t/date-comparison-in-asterisk/69582/2 "2017-01-31T22:40:39Z")

</div>

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

${EPOCH}

---

<div class="post-metadata">

**Author:** ![oladapoadebowale](https://sea1.discourse-cdn.com/asterisk/user_avatar/community.asterisk.org/oladapoadebowale/32/1813_2.png) [@oladapoadebowale](https://community.asterisk.org/u/oladapoadebowale)\
**Post date:** [January 31, 2017, 10:58pm UTC](https://community.asterisk.org/t/date-comparison-in-asterisk/69582/3 "2017-01-31T22:58:19Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![david551](https://avatars.discourse-cdn.com/v4/letter/d/3bc359/32.png) [@david551](https://community.asterisk.org/u/david551)\
**Post date:** [January 31, 2017, 11:04pm UTC](https://community.asterisk.org/t/date-comparison-in-asterisk/69582/4 "2017-01-31T23:04:02Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![oladapoadebowale](https://sea1.discourse-cdn.com/asterisk/user_avatar/community.asterisk.org/oladapoadebowale/32/1813_2.png) [@oladapoadebowale](https://community.asterisk.org/u/oladapoadebowale)\
**Post date:** [January 31, 2017, 11:05pm UTC](https://community.asterisk.org/t/date-comparison-in-asterisk/69582/5 "2017-01-31T23:05:23Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![oladapoadebowale](https://sea1.discourse-cdn.com/asterisk/user_avatar/community.asterisk.org/oladapoadebowale/32/1813_2.png) [@oladapoadebowale](https://community.asterisk.org/u/oladapoadebowale)\
**Post date:** [January 31, 2017, 11:10pm UTC](https://community.asterisk.org/t/date-comparison-in-asterisk/69582/6 "2017-01-31T23:10:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![johnkiniston](https://sea1.discourse-cdn.com/asterisk/user_avatar/community.asterisk.org/johnkiniston/32/28_2.png) [@johnkiniston](https://community.asterisk.org/u/johnkiniston)\
**Post date:** [January 31, 2017, 11:17pm UTC](https://community.asterisk.org/t/date-comparison-in-asterisk/69582/7 "2017-01-31T23:17:02Z")

</div>

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})}'`

---

<div class="post-metadata">

**Author:** ![oladapoadebowale](https://sea1.discourse-cdn.com/asterisk/user_avatar/community.asterisk.org/oladapoadebowale/32/1813_2.png) [@oladapoadebowale](https://community.asterisk.org/u/oladapoadebowale)\
**Post date:** [February 1, 2017, 3:24am UTC](https://community.asterisk.org/t/date-comparison-in-asterisk/69582/8 "2017-02-01T03:24:45Z")

</div>

Thank you very much it worked.
