Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Aug 2002
    Posts
    151
    Thanks
    0
    Thanked 0 Times in 0 Posts

    issues with strtotime and date functions..

    I'm trying to add 3 days to a stored starting date:

    PHP Code:
    echo $_SESSION['start_date']."<br>";

    $new_timestamp = ($_SESSION['start_date'] + strtotime("+3 days"));
    $new_date date('Y-m-d'$new_timestamp);

    echo 
    $new_date
    This ouputs:

    2007-02-18
    2007-02-25
    (which is clearly + 7 days?)

    I would expect it to output:

    2007-02-18
    2007-02-21



    Am I missing something?

  • #2
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    The result of strtotime() is a unix timestamp. You need to convert the your $_SESSION['start_date'] variable to a unix timestamp with mktime() before adding the strtotime() result to it.

  • #3
    Regular Coder
    Join Date
    Aug 2002
    Posts
    151
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, that makes sense.

    I've amended the code but it still doesn't seem to work:

    PHP Code:
    echo $_SESSION['start_date']."<br>";

    $start_time mktime($_SESSION['start_date']);
    $start_date $start_time strtotime("+3 days");
    $new_date date('Y-m-d'$start_date);

    echo 
    $new_date
    returns:

    2006-07-01
    1908-06-05

  • #4
    Super Moderator JohnDubya's Avatar
    Join Date
    Nov 2006
    Location
    Missouri
    Posts
    634
    Thanks
    12
    Thanked 18 Times in 18 Posts
    Fumi, do you (or anyone else that reads this) know of a site that explains how to do stuff with the date in PHP? I'm sure I'm not the only one who's confused on this. I've had to work with mktime() and date() and such in the past, but I still don't understand it enough to work with it. I know that mktime() deals with seconds, but I don't know how to change this into a readable date. Thanks for any help you all can provide.

  • #5
    UE Antagonizer Fumigator's Avatar
    Join Date
    Dec 2005
    Location
    Utah, USA, Northwestern hemisphere, Earth, Solar System, Milky Way Galaxy, Alpha Quadrant
    Posts
    7,691
    Thanks
    42
    Thanked 637 Times in 625 Posts
    Welll..... I can't really put it any better than the manual... it's all about boiling down a date or time to a common denominator. mktime() returns the number of seconds elapsed between January 1 1970 (Unix epoch) and the values you plug into the function.

    Once you boil date/times down to that same common denominator, you can perform addition and subtraction on the value and then convert that mktime value back to a readable date and time using the date() and time() functions.

    Quadrant6 you must pass mktime() the month, day and year values separately.

    PHP Code:
    //assuming date format of YYYY-MM-DD
    $start_time mktime(1200substr($_SESSION['start_date'], 52), substr($_SESSION['start_date'], 82), substr($_SESSION['start_date'], 04)); 
    Note the hour of 12 noon, to avoid issues with time zones, though it may not avoid the issues entirely.

  • #6
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    You want something like:

    PHP Code:
    <?php
    $time_a 
    '2006-07-01';

    $time_b date('Y-m-d',strtotime('+ 3 days',strtotime($time_a)));

    echo 
    $time_b;

    ?>
    YYYY-MM-DD is an acceptable input to strtotime, so you don't need mktime (which takes separate arguments for each portion of the date/time), and the 2nd argument to strtotime can be a timestamp to start from, rather than defaulting to now.

    the above split up slightly for clarity:
    PHP Code:
    $time_a '2006-07-01'//start time

    $time_a_as_timestamp strtotime($time_a); //turn it into a ts

    $time_b_as_timestamp strtotime('+3 days',$time_a_as_timestamp); // add 3 days to time_a

    $time_b date('Y-m-d',$time_b_as_timestamp); //then format 
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #7
    Regular Coder
    Join Date
    Aug 2002
    Posts
    151
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks - that works perfectly.

    As a side note: I'm using it later in an If comparison to check whether the current row date is newer than this $new_date

    PHP Code:
    while($row mysql_fetch..

    if(
    $row['date'] >= $new_date){
    ... 
    Which for example does: if '2006-02-13' >= '2006-03-10'

    I'm surprised that this comparison seems to work? I thought I'd have to convert both again to timestamps or numbers but PHP just knows how to handle (though I'm posting this just in case I'm wrong and missing something )


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •