Question: This is some working code that I wrote to call a JSON file and cache it on my servers. 

I'm calling the cached file. If the file exists I use json\_decode on it. If the file doesn't exist I then call the JSON and decode that. Then after calling the JSON url, I write the contents to the cached file url.

```
$cache = @file_get_contents('cache/'.$filename.'.txt');

 //check to see if file exists:
if (strlen($cache)<1) {

    // file is empty
    echo '<notcached />';
    $JSON1= @file_get_contents($url);

    $JSON_Data1 = json_decode($JSON1);

    $myfile = fopen('cache/'.$filename.'.txt', "w");
    $put = file_put_contents('cache/'.$filename.'.txt', ($JSON1));

} else {

    //if file doesn't exist:
    $JSON_Data1 = json_decode($cache);
    echo '<cached />';
}

```

Instead of only using `if (strlen($cache)<1) {`, is there a way that I can check the age of the $filename.txt and if it's older than 30 days grab the JSON url in the else statement?

Answer: 
The behaviour of `atoi(test);` is *undefined*. The parameter `test` (once decayed to a pointer type) must point to a NUL-terminated array of `char`. Yours isn't.

The solution is trivial here, write

```
char test[9] = {0};

```

instead to force the inclusion of a NUL-terminator . But do note that the resulting number is of the order of 10 million; perhaps therefore too big for an `int`. Use a `long` instead to be sure, along with `atol` instead of `atoi`. If you want the string to be parsed as binary, then use `strtol` passing a radix of 2.
To convert a number formatted as string in base 2 ("binary") to an actual integer, use [`strtoul()`](https://linux.die.net/man/3/strtoul):

```
const unsigned long x = strtoul("11100011", NULL, 2);
printf("x=%lu\n", x);

```

The final `2` there specifies the base, and `2` of course gives binary, and this prints `227`.