I need to put a date in my database that is the current time plus 14 days... How do I do this?? time() + 14 ?? thanks!
Let's say you have the date in $d. $d = mktime(0,0,0,$month,$day,$year); $end = date(â€Y m dâ€,strtotime(â€+14 daysâ€,$d)); Code (markup): It should work just fine!
Another way to do it would to make use of the fact a day is 86400 seconds so you could also have done time() + 86400*14
[COLOR="DarkRed"]<?php[/COLOR] [COLOR="DarkRed"]$oldDate[/COLOR] = [COLOR="Green"]'02/20/2008'[/COLOR]; [COLOR="DarkRed"]$newDate[/COLOR] = [COLOR="Blue"]new[/COLOR] DateTime([COLOR="DarkRed"]$oldDate[/COLOR]); [COLOR="DarkRed"]$newDate[/COLOR]->modify([COLOR="Green"]'+14 Days'[/COLOR]); [COLOR="Blue"]echo[/COLOR] [COLOR="DarkRed"]$newDate[/COLOR]->format([COLOR="Green"]'m/d/Y'[/COLOR]); Code (markup):
$date = "07-20-08 02:54:03 AM"; echo date("m-d-y h:i:s A", strtotime ("+14 days")); PHP: this was help for another post but you get the idea
strtotime() is one of the slowest PHP functions, so the above method would be most appropriate whenever possible.
It should be faster than any other solution posted that is PHP based I would imagine. Reason being it's simply math while the others are manipulating strings in one way or another. Only issue I can see with my solution is the fact it has 86400 which may not be clear to the person looking at the code. But most programmers know things like 3600 seconds in an hour 86400 seconds in a day and 604800 seconds in a week. Also worth noting when possible it's best to handle date and time manipulation on the database end. Things like where statements and ranges can be much cleaner and more powerful if handled then. Lots of scripts are storing unix timestamps which were great for flat files systems but most database engines have very good date and time systems. For example if you have a forum and are only looking for topics created on Tuesdays this can be tricky thing to figure out and do optimally on the database end (not grabbing all records). While doing it via a query of a date field is very easy to do. A bit of a rant but I figured I'd mention this since people are talking about manipulating unix time which unless you are specifically calling time() can be done in much better ways without php at all in most cases.