You can start from the following script
<?php $arr[] = '2016-09-02_20-47-22.jpg'; $arr[] = '2016-09-02_19-42-02.gif'; $arr[] = '2016-08-23_20-37-42.png'; $arr[] = '2016-08-17_20-17-22.jpg'; foreach($arr as $filename) { $pattern = '/^(\d{4})-(\d{2})-(\d{2})_(\d{2})-(\d{2})-(\d{2})/'; if(preg_match($pattern, $filename, $out)) { if(mktime($out[4], $out[5], $out[6], $out[2], $out[3], $out[1]) < time()) { unlink($filename); } } }
Instead of the $arr
array and the foreach()
you can use the glob()
function as a source for the file list source, for example, using the glob()
function.
<?php foreach (glob("*") as $filename) { $pattern = '/^(\d{4})-(\d{2})-(\d{2})_(\d{2})-(\d{2})-(\d{2})/'; if(preg_match($pattern, $filename, $out)) { if(mktime($out[4], $out[5], $out[6], $out[2], $out[3], $out[1]) < time()) { unlink($filename); } } }
If the time in the file should not be taken into account, then instead of the date and time
mktime($out[4], $out[5], $out[6], $out[2], $out[3], $out[1])
you can only generate the date by zeroing the first three parameters of the mktime()
function
mktime(0, 0, 0, $out[2], $out[3], $out[1])