Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get date in php in 2 weeks

Tags:

php

datetime

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

like image 661
anonymous123 Avatar asked Aug 27 '10 08:08

anonymous123


3 Answers

$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
like image 66
jensgram Avatar answered Nov 16 '22 23:11

jensgram


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")+14, date("Y")));
like image 22
shamittomar Avatar answered Nov 17 '22 00:11

shamittomar


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;
like image 4
Gumbo Avatar answered Nov 17 '22 00:11

Gumbo