But it does not brings the right values(not always)
For instance when the interval 2days it returns 2
But when the interval is 45days (now-45d) the variable $__interval returns 30

The $__interval is calculated using the time range and the width of the graph (the number of pixels).

Approximate Calculation: (to - from) / resolution

For example, when the time range is 1 hour and the graph is full screen, then the interval might be calculated to 2m - points are grouped in 2 minute intervals. If the time range is 6 months and the graph is full screen, then the interval might be 1d (1 day) - points are grouped by day.

In the InfluxDB data source, the legacy variable $interval is the same variable. $__interval should be used instead.

The InfluxDB and Elasticsearch data sources have Group by time interval fields that are used to hard code the interval or to set the minimum limit for the $__interval variable (by using the > syntax → >10m ).

Excellent. I found what I looked for.
Actually it should be something like Round((($__to) - ($__from) ) / (1000 * 60 * 60 * 24), 1) to get the dashboard selected time range in days.