Heis
Heis

Reputation: 568

PHP rename with date stamp

we want to make a script that renames the file name with a date stamp, we're new to PHP so this is what we have now.

<?php rename("test.txt", "tes3.txt"); ?>

we tried some with things like this below but we don't get it to work.

$date = new DateTime();
echo $date->format('Y-m-d H:i:sP') . "\n";

I hope you guys have a answer how we can do this. Thanks

Upvotes: 2

Views: 4449

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476584

All you have to do is use the textual output (string) produced by $date->format(...) and inject the answer in the rename call.

$date = new DateTime();
rename("test.txt", "test" . $date->format('Y-m-d H:i:sP') . ".txt");

But it is advisable to not use any spaces in filenames, especially if you want the files to be accessible through a server/website/...

Upvotes: 4

Related Questions