$addhr = time() + (1 * 13 * 60 * 60);
$curDateTimeMil= date("Y-m-d G:i:s",$addhr);
echo $curDateTimeMil;
This will echo 2010-08-27 16:21:31
.
How can I get the date after 2 weeks? (sept 10)
$addhr = time() + (1 * 13 * 60 * 60);
$curDateTimeMil= date("Y-m-d G:i:s",$addhr);
echo $curDateTimeMil;
This will echo 2010-08-27 16:21:31
.
How can I get the date after 2 weeks? (sept 10)
$dateInTwoWeeks = strtotime('+2 weeks');
See strtotime()
.
Update #1: Yes, this is the lazy way to do it. However, I do believe that the possible negative performance impact is countered by the positive effect on readability / understanding of the code. Should performance be an issue, one could switch to native (integer-based) time manipulation (and add comments).
Update #2: The optional second argument is the reference date. E.g.:
strtotime('+2 weeks', mktime(0, 0, 0, 2, 8, 1984)); // 8th Feb. 1984 + 2 weeks
You can specify the starting time (from where to calculate) using mktime()
.
Example: Two week after September 10, 2010 (i.e. +14 days):
$date = date("Y-m-d", mktime(0, 0, 0, 9, 10 + 14, 2010);
To get just the DATE (not time) of two weeks later (+14 days) from today:
$date = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d")+14, date("Y")));
And this gives with TIME of two weeks later (+14 days) from now:
$date = date("Y-m-d G:i:s", mktime(date("G"), date("i"), date("s"), date("m"), date("d")-2, date("Y")));
You could simply add the number seconds of two weeks:
2 weeks = 2 · 7 days = 14 days
= 14 · 24 hours = 336 hours
= 336 · 60 minutes = 20160 minutes
= 20160 · 60 seconds = 1209600 seconds
So:
$curDateTimeMil += 1209600;
// or
$curDateTimeMil += 2 * 7 * 24 * 60 * 60;