Date and time before extension

27.Oct.2010

Script authors: Drasko Gomboc and Sankovic Marko.

When running automated backup it is a good practice to save exact time when you made backup in your file name. This snippet shows you how to automatically add date and time before extension in your file name.

1
2
3
$filepath = '/backup/folder/drasko_gomboc-and-sankovic_marko.txt';
$new_filepath = preg_replace("/(.[^.]*)$/", date('-Y-m-d-h-i-s') . "$1", $filepath); // Add date and time before file extension
echo $new_filepath; //output should look like this -> /backup/folder/drasko_gomboc-and-sankovic_marko-2010-10-27-09-00-00.txt

Script has been provided by generous guys and great developers: Drasko Gomboc and Sankovic Marko.