The approach you've tried will not work - you're mixing data definition statements with data manipulation statements.
You could probably do it in a stored procedure in MySQL, but I haven't tried it.
So you're faced with a choice: do it in MySQL via a stored procedure (which, to my mind, kind of hides the implementation) or do it in PHP (where the code will probably be more accessible and understandable).
Note that, in either event, you'll probably need to lock the tables to prevent the table from disappearing between the time when you query it (to find the timestamp value) and the time when you drop it.