views:

84

answers:

4

I need to make a timestamp to put into MySQL. The user is submitting a number (of weeks) I need to add that many weeks to today's date. What I am trying to do is calculate an end date of an ad that the user is submitting.

Any suggestions on how to do this? Thanks!

+3  A: 

You can use strtotime to add time to a date - it takes a second argument that is the current time if none is passed. You can then pass that created time to the date function to create your timestamp:

$timestamp = date('Y-m-d H:i:s', strtotime('+10 weeks'));
Paolo Bergantino
+2  A: 

I think DATE_ADD(CURDATE(), INTERVAL 2 WEEK) would add 2 weekss to the current date, for instance

San Jacinto
A: 

If you want the ad to expire at a certain time of day you can use mktime:

$day = date("d") + ($weeks * 7);
mktime($hour,$minute,$second,$month,$day,$year);

Eddy
A: 

Just to add, some would say that storing the UNIX time as a plain-old int field in the database is the more flexible and portable solution. Furthermore, inserts and updates happen faster because they only involve storing simple integers. It really depends on how much date manipulation you need to do at the database level. I tend to go for portability and do all my date calculations in PHP. To store the current timestamp, I would just insert into an integer column the output of:

strtotime('now');

or

time();

which both return the current (unix) timestamp. Date comparison thereafter can be done by fetching timestamps from the database and performing simple arithmetical operation, as trivial as:

if($tsFromDb > strtotime('+28 days')) {
   echo 'it is the future, zombies!';
}

It really depends on what you're using dates for.

karim79