tags:

views:

56

answers:

3
$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)

+6  A: 
$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
jensgram
how can i specify the start date before obtaining the date in 2 weeks?
anonymous123
By accepting answers to some of your previous questions :-)Or reading the documentation. Or using `time()`.
fredley
@anonymous123 See update #2.
jensgram
+2  A: 

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")));
shamittomar
+1  A: 

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;
Gumbo
With the tiny caveat that you may miss the right day if you're unlucky enough to fall into a daylight savings switchover or leap second... :)
deceze
@deceze: That’s not an issue with the time but with the representation of the time.
Gumbo