Since you want to work with clearing folders based on a date I would suggest that this will be easier by using standard Unix commands, assuming that your system is unix based.
If you look into the function find
you will see that you can specify the time of creation by the option mtime
.
An example of this is:
find /path/to/files* -mtime +5
This will find files that are older than 5 days. You can then use the option exec
to call a command on the files found. In your case you could call rm
and that would remove the files. An example of this would be:
find /path/to/files* -mtime +5 -exec rm {} \;
NOTE: be careful before you run anything that can remove files