如何在php中的函数上设置24小时的时间延迟?

I have a web application written in php5.

The application allows users to register and I would like to delay when the various details such as password and links will be sent out. I would like the email containing this information to go 24h after a user has resisted.

How can I to execute the function that will send the email after 24 hours has passed?

It would depend on how accurate '24' hours needs to be, but something like the following approach should work.

Create a table to hold user registrations, or adapt an existing table to hold a registrationTime column. Make this column nullable DATETIME you will use it to hold the HH:MM of the users registration. The new (or modified) table should also have the data relevant for the emails, or a common identifier for users, allowing the correct data to be selected.

Finally, you would set up a cron job to query the data base every x mins and select the users who registered within y mins of 24hours. Where x is how often you want the job to run and y would be some degree of tolerance for how close to 24:00 hours had elapsed.

For any results you would construct and send the emails. Finally you would set some a flag to show that user had been dealt with - a good way would be setting the registrationTime to null for all users who have had the email sent.

Here are a few links to help you out:

Compare time in SQL Server How can I compare time in SQL Server?

Introducing Cron http://www.sitepoint.com/introducing-cron/

I would setup database table that handles your email queue. This table can have a column that specifies when each row, corresponding to one email, was queued up. You can have a cron job running, let's say every hour or so, that checks if the 24 hours has elapsed.

email_type   | to             | data
--------------------------------------------------------------------------
registration | fred@stuff.com | {first_name: "Fred", last_name: "Stuff" }

This solution is flexible because you might have other types of emails that you want delayed by different amounts of time. You can add a column that indicates what type of email and your code handles each type differently.

This cronjob will run every hour:

0 * * * * php check_mail_queue.php

You can either add a column that indicates whether an email's been sent or simply delete the row. Also, if you're going to have custom data in each email, serialize the array, hash, dictionary, whatever, and store that into a column in the table too.