Use mktime to generate 1 a timestamp one month ahead???

Discussion in 'PHP' started by MyFlash13, Aug 25, 2008.

  1. #1
    I'm an experienced PHP user, but I have never quite grasped mktime..

    How do I use mktime to generate a timestamp which is one month in the future?
     
    MyFlash13, Aug 25, 2008 IP
  2. logylaps

    logylaps Active Member

    Messages:
    761
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    85
    #2
    Try this:
    <?php
    echo mktime(date("g"),date("i"),date("s"),date("n")+1);
    ?>
    PHP:
     
    logylaps, Aug 25, 2008 IP
  3. AimHigher

    AimHigher Member

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #3
    You don't need to use mktime for this.

    
    $now = time();
    $seconds_in_a_month = 60 * 60 * 24 * 31;
    $one_month_in_the_future = $now + $seconds_in_a_month;
    
    Code (markup):
    All you do is add the number of seconds in a month to the current timestamp and it will give you a timestamp for one month in the future.

    An example of when you might use mktime() is if the user has selected the day month and year from a dropdown and you want to convert it into a timestamp before storing it in a database:

    
    // Let's just pretend you've already validated your form data
    $birthday = mktime(0, 0, 1, $_POST['month'], $_POST['day'], $_POST['year']);
    
    Code (markup):
     
    AimHigher, Aug 25, 2008 IP
  4. ansi

    ansi Well-Known Member

    Messages:
    1,483
    Likes Received:
    65
    Best Answers:
    0
    Trophy Points:
    100
    #4
    uhhhhh
    
    echo date("F d, Y g:i A", strtotime( "+ 1 month" ) );
    
    PHP:
     
    ansi, Aug 25, 2008 IP
  5. oo7ml

    oo7ml Well-Known Member

    Messages:
    656
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #5
    <?php
    
    $next_month = mktime(0,0,0,date("m")+1,date("d"),date("Y"));
    
    echo "In one month's time, it will be: ".date("D / M / Y", $next_month);
    
    ?>
    PHP:
     
    oo7ml, Aug 26, 2008 IP
  6. AimHigher

    AimHigher Member

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #6
    What value does this return if the month is december?
     
    AimHigher, Aug 26, 2008 IP
  7. oo7ml

    oo7ml Well-Known Member

    Messages:
    656
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #7
    January???
     
    oo7ml, Aug 27, 2008 IP
  8. wdillsmith

    wdillsmith Peon

    Messages:
    37
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I always use strtotime and let PHP figure it out for me.
     
    wdillsmith, Aug 27, 2008 IP
    ansi likes this.
  9. AimHigher

    AimHigher Member

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #9
    12+1 = 13 ?
     
    AimHigher, Aug 27, 2008 IP
  10. oo7ml

    oo7ml Well-Known Member

    Messages:
    656
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #10
    No, it shows as January
     
    oo7ml, Aug 28, 2008 IP