datetime - HELP needed

Discussion in 'PHP' started by KingCobra, Feb 22, 2009.

  1. #1
    In mysql i store datetime like "2009-01-13 21:14:53" format.

    1. When I call this date from php I want to add 15 days with that value and display the new datetime (including 15 days). Its may look like near "2009-01-28 21:14:53".

    2. Before displaying the result of 'question-1' I want subtruct that result from current datetime and display the new result in days.

    let current datetime is "2009-01-20 20:12:33"

    Calcsequence : current datetime - (posted datetime + 15 days)

    The result will be something like - 8 days

    3. Can I post current datetime from php something like this "5436543652" instead of "2009-01-20 20:12:33" ? If yes HOW ?

    4. If questions-3 is YES how can i retrive again datetime like "2009-01-20 20:12:33" form data field "5436543652"?

    PLEASE HELM ME......
     
    KingCobra, Feb 22, 2009 IP
  2. ActiveFrost

    ActiveFrost Notable Member

    Messages:
    2,072
    Likes Received:
    63
    Best Answers:
    3
    Trophy Points:
    245
    #2
    <?php
    $timestamp = time(); // current timestamp
    $15days = 86400 * 15; // 86400 is the same as 24 hours
    $result = $timestamp + $15days;
    echo "Now : ".$timestamp."<br>";
    echo "15 days later : ".$result."<br>";
    ?>
    PHP:
     
    ActiveFrost, Feb 22, 2009 IP
  3. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #3
    1. DATE_ADD()
    2. DATEDIFF()
    3. NOW()
    4. FROM_UNIXTIME()

    RTFM
     
    wmtips, Feb 22, 2009 IP
  4. xrvel

    xrvel Notable Member

    Messages:
    918
    Likes Received:
    30
    Best Answers:
    2
    Trophy Points:
    225
    #4
    The first thing that you should do is convert '2009-01-13 21:14:53' (string) into timestamp (integer).
    If you've done it, you can do anything you want, get 15 days before it, 5 days after it, 5 hours after it, etc.

    First, convert string to integer
    <?php
    $yourDate = '2009-01-13 21:14:53';// this is your date
    $yourTimeStamp = strtotime($yourDate);// lets convert it to timestamp -- integer
    ?>
    PHP:
    Next, for example you want 5 days after $yourDate.
    We have to convert 5 days into seconds.
    Which means
    seconds = 5 * 24 * 3600.

    So here is 5 days after $yourDate
    <?php
    $secondsModification = 5 * 24 * 3600;
    
    // why i add, not substract, because it happens _AFTER_ a date
    // if it _BEFORE_ a date, substract it
    $newTimeStamp = $yourTimeStamp + $secondsModification;
    ?>
    PHP:
    Ok we've had the new timestamp, now you can display it on friendly date format
    <?php
    echo 'Yay ! '.date('r', $newTimeStamp);
    ?>
    PHP:
    About question number 3 & 4, i am not sure.
    Why don't you try it lol.
    If the inserted datetime is 0000-00-00, it means you can't :D
    Less than 1 minute to try :)
     
    xrvel, Feb 22, 2009 IP
  5. ads2help

    ads2help Peon

    Messages:
    2,142
    Likes Received:
    67
    Best Answers:
    1
    Trophy Points:
    0
    #5
    @activefrost, I think variables cannot start with number.
    Maybe change $15days to $fifteendays.

    Back to topic,

    Like how the above poster said, you can add seconds which is equivalent to the 15 days to the timestamp (which is also in seconds)

    or

    You can do this

    
    $yourDate = '2009-01-13 21:14:53';
    $after_15_days = strtotime($yourDate." + 15 days");
    
    PHP:
    To get it back in YYYY-MM-DD H:I:S format,
    
    $latest_date = date("Y-m-d H:i:s",$after_15_days);
    
    PHP:
    - ads2help
     
    ads2help, Feb 22, 2009 IP
  6. KingCobra

    KingCobra Well-Known Member

    Messages:
    289
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    103
    #6
    Thank you all
     
    KingCobra, Mar 2, 2009 IP