adding 14 days to time()

Discussion in 'PHP' started by jkashu, Jul 21, 2008.

  1. #1
    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!
     
    jkashu, Jul 21, 2008 IP
  2. kmofo

    kmofo Active Member

    Messages:
    442
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    85
    #2
    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!
     
    kmofo, Jul 21, 2008 IP
  3. jkashu

    jkashu Peon

    Messages:
    39
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thanks! that works great!
     
    jkashu, Jul 21, 2008 IP
  4. InFloW

    InFloW Peon

    Messages:
    1,488
    Likes Received:
    39
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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
     
    InFloW, Jul 21, 2008 IP
  5. ahowell

    ahowell Peon

    Messages:
    38
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    
    [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):
     
    ahowell, Jul 21, 2008 IP
  6. cornetofreak

    cornetofreak Peon

    Messages:
    170
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #6
    
    
    $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
     
    cornetofreak, Jul 21, 2008 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    strtotime() is one of the slowest PHP functions, so the above method would be most appropriate whenever possible.
     
    nico_swd, Jul 21, 2008 IP
  8. lfhost

    lfhost Peon

    Messages:
    232
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #8
    
    $date = mktime(0,0,0,date("m"),date("d")+14,date("Y"));
    echo date("Y m d",$date);
    
    Code (markup):
     
    lfhost, Jul 21, 2008 IP
  9. InFloW

    InFloW Peon

    Messages:
    1,488
    Likes Received:
    39
    Best Answers:
    0
    Trophy Points:
    0
    #9

    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.
     
    InFloW, Jul 21, 2008 IP