How do I get 3 sec after this timestamp

Discussion in 'PHP' started by Imozeb, May 27, 2010.

  1. #1
    This is queryed from a database (the row is set to current_timestamp) and it seems to be outputing
    2010-05-27 10:18:38

    I need to convert this to a timestamp and then get three seconds ahead of that time so I can compare it to another timestamp. How would I do this?

    Thank you.
     
    Imozeb, May 27, 2010 IP
  2. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #2
    
    $date = new DateTime('2010-05-27 10:18:38');
    $timestamp = $date->getTimestamp();
    $newTimestamp = $timestamp + 3;
    $newDate = new DateTime();
    $newDate->setTimestamp($newTimestamp);
    
    PHP:
     
    Gray Fox, May 27, 2010 IP
  3. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #3
    <?php
    $date = '2010-05-27 10:18:38';
    //convert $date to a timestamp...
    $date_to_timestamp = strtotime($date);
    //add 3...
    $new_date = $date_to_timestamp + 3;
    
    //output the new date...
    echo $new_date;
    ?>
    PHP:
     
    danx10, May 27, 2010 IP
  4. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Thanks. Is there anyway to create a variable which is shared by everyone using my .php script? Kind of like session but with everyone?
     
    Imozeb, May 27, 2010 IP
  5. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #5
    Ellaborate the purpose?
     
    danx10, May 27, 2010 IP
  6. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    It's for a function which must run constantly, like a cron job, but the only difference is that it must only run if people are using one of my .php scripts. The function will delete rows from a database every 5 seconds. The problem is that I only want it to do this if there are people using a different .php script.

    Hope I explained it okay.

    Thanks!
     
    Imozeb, May 27, 2010 IP
  7. Lord

    Lord Active Member

    Messages:
    134
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #7
    if you want data shared between multiple users, use a tabel in your database, store the variable in it, and in your script retrieve it everytime, check it against your conditions and update it if that's the case
     
    Lord, May 27, 2010 IP
  8. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    I was thinking about doing that but I was wondering if PHP had a faster better way to do that function?
     
    Imozeb, May 27, 2010 IP
  9. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #9
    Temp file storage may be easier
    
    //$temp - variable you want to store
    file_put_contents('tempFileName', serialize($temp));
    
    //when you want to read the temp file from any other file
    $temp = unserialize(@file_get_contents('tempFileName'));
    
    PHP:
     
    Gray Fox, May 28, 2010 IP
  10. flexdex

    flexdex Peon

    Messages:
    104
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #10
    You may want to skip that php parsed calculations by just requesting what you really want

    SQL
    
      select UNIX_TIMESTAMP(date_add(now(), interval 3 second))
    
    Code (markup):
    Regards
     
    flexdex, May 28, 2010 IP
  11. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #11
    I would use APC or memcached to store a persistent variable. If you're not using APC it's a good idea to install it anyway.

    apc_add('foo', $bar);

    apc_fetch('foo');
     
    jestep, May 28, 2010 IP