Timestamp and Dates

Discussion in 'PHP' started by sweetfunny, Nov 20, 2007.

  1. #1
    I have timestamps in a database table and i'm having trouble trying to work out how to display the right values.

    The timestamp in the DB is for example: 1195841962

    All the date values are in the future, so i'm trying to output them in the format: 30 Days 4 Hours 35 Minutes.

    Which would be that many days ahead of today. Seconds are optional, it can be rounded to minutes.

    Please help, i've fried my last remaining brain cell on php/MySql documents. :eek:
     
    sweetfunny, Nov 20, 2007 IP
  2. serialCoder

    serialCoder Guest

    Best Answers:
    0
    #2
    i dont think mysql has a built in function for this, but i suppose you can create(or search the web) a php function that can do this.
     
    serialCoder, Nov 20, 2007 IP
    sweetfunny likes this.
  3. sweetfunny

    sweetfunny Banned

    Messages:
    5,743
    Likes Received:
    467
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I have been for a few hours now, and smoked my brain cells :eek:

    From what i gather, i need to get the current server time as a Unix Timestamp and subtract it from the future Timestamp that's stored in the database. That will give the time difference in Unix, which when converted to human time will be the value in Days, Hours, Minutes.

    Unfortunately my ambition and my abilities are two different things. :D
     
    sweetfunny, Nov 20, 2007 IP
  4. serialCoder

    serialCoder Guest

    Best Answers:
    0
    #4
    yep, actually the current timestamp can be generated using mktime(), i can probably create/find/modify a function for you, i'm just in the middle of some things that i need done, then if it's not resolved yet, i'll post it here :)
     
    serialCoder, Nov 20, 2007 IP
  5. sweetfunny

    sweetfunny Banned

    Messages:
    5,743
    Likes Received:
    467
    Best Answers:
    0
    Trophy Points:
    0
    #5
    That would be excellent, thankyou. :)

    I'm still reading and trying things, but i'm not even remotely close to a solution.
     
    sweetfunny, Nov 20, 2007 IP
  6. salmanshafiq

    salmanshafiq Well-Known Member

    Messages:
    260
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    128
    #6
    use this code and if any problem then tell me

    
    
    $timestamp1=1195841962;
    echo duration($timestamp1-mktime(date("H"),date("i"),date("s"),date("m"),date('d'),date('Y')));
    function duration( $int_seconds=0, $if_reached=null )
    {
    
       $key_suffix = 's';
       $periods = array(
                       'year'        => 31556926,
                       'month'        => 2629743,
                       'day'        => 86400,
                       'hour'        => 3600,
                       'minute'    => 60,
                       'second'    => 1
                       );
    
       // used to hide 0's in higher periods
       $flag_hide_zero = true;
    
       // do the loop thang
       foreach( $periods as $key => $length )
       {
           // calculate
           $temp = round( $int_seconds / $length );
    
           // determine if temp qualifies to be passed to output
           if( !$flag_hide_zero || $temp > 0 )
           {
               // store in an array
               $build[] = $temp.' '.$key.($temp!=1?'s':null);
    
               // set flag to false, to allow 0's in lower periods
               $flag_hide_zero = false;
           }
    
           // get the remainder of seconds
           $int_seconds = fmod($int_seconds, $length);
       }
    
       // return output, if !empty, implode into string, else output $if_reached
       return ( !empty($build)?implode(', ', $build):$if_reached );
    }
    
    
    PHP:
     
    salmanshafiq, Nov 20, 2007 IP
    sweetfunny likes this.
  7. salmanshafiq

    salmanshafiq Well-Known Member

    Messages:
    260
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    128
    #7
    Note that: you will recieve better results by not using floor and using round instead. As you continue increasing to larger amounts of time you will notice that the outputted time is off by large amounts.

    so instead of $temp = floor( $int_seconds / $length );
    we would use $temp = round( $int_seconds / $length );
     
    salmanshafiq, Nov 20, 2007 IP
  8. serialCoder

    serialCoder Guest

    Best Answers:
    0
    #8
    hi, i tested it and i think it works great except for this line

    return ( !empty($build)?implode(', ', $build):$if_reached );

    just remove the opening '(' and closing ')'
     
    serialCoder, Nov 20, 2007 IP
  9. sweetfunny

    sweetfunny Banned

    Messages:
    5,743
    Likes Received:
    467
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Excellent, thanks for the code. Now for a stupid question, how do i use it?

    Do i save it as whatever.php and include that file in my script? Also do i have to put the database table name in there somewhere?

    Thanks. :D
     
    sweetfunny, Nov 20, 2007 IP
  10. serialCoder

    serialCoder Guest

    Best Answers:
    0
    #10
    well its up to you on how you want to implement it but i suggest saving it in a file because including it on your main file will look messy

    assuming you have queried your db this is how you use it

    assigning to a variable
    $var = duration(resultfromdb-mktime);

    displaying
    echo duration(resultfromdb-mktime);

    the resultfromdb is the value you got in your db containing a timestamp
     
    serialCoder, Nov 20, 2007 IP
    Pammer likes this.
  11. legend2

    legend2 Well-Known Member

    Messages:
    1,537
    Likes Received:
    74
    Best Answers:
    0
    Trophy Points:
    115
    #11
    will write a function for you.
    just a clarification: the timestamps you have are in the future and you want to know how much hours,mins,secs are elft from now until that timestamp right?
     
    legend2, Nov 21, 2007 IP
    sweetfunny likes this.
  12. legend2

    legend2 Well-Known Member

    Messages:
    1,537
    Likes Received:
    74
    Best Answers:
    0
    Trophy Points:
    115
    #12
    here you go

    
    <?
    //function that does the job. save it as timeleft.php
    function timeleft($timestamp) {
        $current = time();
        $diff = $timestamp - $current;
        if($diff<0)
            $diff = 0;
            
        $daysleft = floor($diff/86400);
        $hoursleft = floor(($diff-($daysleft*86400))/3600);
        $minutesleft = floor(($diff-($daysleft*86400)-($hoursleft*3600))/60);
        $secondsleft = $diff-($daysleft*86400)-($hoursleft*3600)-($minutesleft*60);
        
        if($diff)
            echo "<b>Time left</b>: $daysleft days, $hoursleft hours, $minutesleft minutes, $secondsleft seconds";
        else
            echo "<b>Timestamp already reached</b>.";
    }
    ?>
    
    PHP:
    
    <?
    //example use
    include "timeleft.php";
    timeleft(1195841962);
    ?>
    
    PHP:
     
    legend2, Nov 21, 2007 IP
  13. sweetfunny

    sweetfunny Banned

    Messages:
    5,743
    Likes Received:
    467
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Yes, spot on.

    The times in the future are less than a month, so it will be like 10 days, 5 Hours, 30 Minutes

    At the exact same time tomorrow, it should show 9 Days, 5 Hours, 30 Minutes

    When it gets to zero seconds, the script is already built to remove the entry from the database so the time counter doesn't need to function for time in the past.

    Edit: Legend2 that works perfectly, so how do i get it to run against the timestamp values in the database table?

    Getting real close now :D
     
    sweetfunny, Nov 21, 2007 IP
  14. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #14
    EDIT: Too late. This didn't seem to be solved when I stated replying.

    
    function calc_date_diff($timestamp_past = false, $timestamp_future = false, $years = true, $months = true, $days = true, $hours = true, $mins = true, $secs = true)
    {
        // Use current time if parameter is FALSE.
        if (!$timestamp_past)   $timestamp_past = time();
        if (!$timestamp_future) $timestamp_future = time();
       
        // Attempt to convert strings to timestamp if necessary.
        $timestamp_future = is_string($timestamp_future) ? strtotime($timestamp_future) : intval($timestamp_future);
        $timestamp_past   = is_string($timestamp_past)   ? strtotime($timestamp_past)   : intval($timestamp_past);
       
        $diff = $timestamp_future - $timestamp_past;
        $calc_times = array();
        $timeleft   = array();
       
        // Prepare array, depending on the output we want to get.
        if ($years)  $calc_times[] = array('Year',   'Years',   31104000);
        if ($months) $calc_times[] = array('Month',  'Months',  2592000);
        if ($days)   $calc_times[] = array('Day',    'Days',    86400);
        if ($hours)  $calc_times[] = array('Hour',   'Hours',   3600);
        if ($mins)   $calc_times[] = array('Minute', 'Minutes', 60);
        if ($secs)   $calc_times[] = array('Second', 'Seconds', 1);
    
        foreach ($calc_times AS $timedata)
        {
            list($time_sing, $time_plur, $offset) = $timedata;
           
            if ($diff >= $offset)
            {
                $left = floor($diff / $offset);
                $diff -= ($left * $offset);
                $timeleft[] = "{$left} ". ($left == 1 ? $time_sing : $time_plur);
            }
        }
       
        return $timeleft ? ($timestamp_future > $timestamp_past ? null : '-') . implode(', ', $timeleft) : 0;
    }
    
    PHP:
    
    
    echo calc_date_diff(false, $your_timestamp);
    
    PHP:
     
    nico_swd, Nov 21, 2007 IP
    sweetfunny likes this.
  15. legend2

    legend2 Well-Known Member

    Messages:
    1,537
    Likes Received:
    74
    Best Answers:
    0
    Trophy Points:
    115
    #15
    sweetfunny, no problem if it's more than a month. And it will also let you know if the timestamp has already passed.

    Regarding how to make it work, you need to connect to the database, query for the timestamp then pass it to the function:)

    If you let me know your db structure and the query condition I can set up the database fetching fro you.
     
    legend2, Nov 21, 2007 IP
  16. sweetfunny

    sweetfunny Banned

    Messages:
    5,743
    Likes Received:
    467
    Best Answers:
    0
    Trophy Points:
    0
    #16
    Thanks, i'm probably someone attempting something i really shouldn't with this.

    The script is connecting to the DB and calling data from the other tables, so i was going to just copy the code for one of these and replace the table name with 'futuredate' which is the one where all the Unix time stamps are stored.

    I don't really know the database structure, unless you mean the schema i imported. :eek:
     
    sweetfunny, Nov 21, 2007 IP
  17. legend2

    legend2 Well-Known Member

    Messages:
    1,537
    Likes Received:
    74
    Best Answers:
    0
    Trophy Points:
    115
    #17
    sweetfunny, here is an example of how you would use it. you need to edit the database info and the query that gives you the timestamp.
    
    <?
    //edit those entries
    $database_host = "localhost";
    $database_name = "database";
    $database_user = "username";
    $database_password = "password";
    $query = "";
    
    
    
    @mysql_connect($database_host,$database_user,$database_password);
    @mysql_select_db($database_name);
    $result = @mysql_query($query);
    $row = @mysql_fetch_array($result);
    $future = $row[0];
    include "timeleft.php";
    timeleft($future);
    ?>
    
    PHP:
     
    legend2, Nov 21, 2007 IP
    Pammer likes this.
  18. sweetfunny

    sweetfunny Banned

    Messages:
    5,743
    Likes Received:
    467
    Best Answers:
    0
    Trophy Points:
    0
    #18
    No go, no matter what i try i'm getting Timestamp already reached. I know the Unix values in the database are in the future because i ran them through 2 online tools to resolve the date.

    I think i might call it quits, if it's not working after 8 hours it's never going to work.

    Reps added to everyone who helped.
     
    sweetfunny, Nov 21, 2007 IP
  19. salmanshafiq

    salmanshafiq Well-Known Member

    Messages:
    260
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    128
    #19
    you have to get only one thing the time stamp form db and assign that to $timestamp1 variable and then write the blow line if any problem then tell me and you can write function on any file which called on that page

    $timestamp1=1195841962;

    echo duration($timestamp1-mktime(date("H"),date("i"),date("s"),date("m"),date('d'),date('Y')));
     
    salmanshafiq, Nov 21, 2007 IP
  20. legend2

    legend2 Well-Known Member

    Messages:
    1,537
    Likes Received:
    74
    Best Answers:
    0
    Trophy Points:
    115
    #20
    sweetfunny, the example you gave is in the future with a month+ time diff.
    If you can show me the database schema along with with an example row, i'll help you out.
     
    legend2, Nov 21, 2007 IP