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 4 of 4

Thread: time conversion

  1. #1
    Regular Coder
    Join Date
    May 2006
    Posts
    107
    Thanks
    1
    Thanked 0 Times in 0 Posts

    time conversion

    Hi

    I am having problem in converting time:

    I have "1196242839" and I want to convert it to find exact time.

    I tired "strptime()" but get error.

    any help

  • #2
    Regular Coder
    Join Date
    May 2006
    Posts
    107
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I have solved the problem
    here is solution
    PHP Code:
     strftime('%d/%m/%Y %H:%M:%S',1196242839
    Now I wanted to know is there any way i can find difference between:

    1196242839 and 1196241839

    Means how many days, years, hours ?

    thanks

  • #3
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Here is something I wrote a while back that should do just what you'd like.
    PHP Code:
    /**
     * date_diff()
     * Calculates the difference between two unix timestamps
     *
     * @param    int      $currTime     UNIX timestamp
     * @param    int      $origTime     UNIX timestamp to compare
     * @param    const    $limit        How deep to drill down
     * @return   string                 The difference in time
     */
    if ( !function_exists'date_diff' ) )
    {
        
    define'DATE_DIFF_YEARS'  );
        
    define'DATE_DIFF_MONTHS' );
        
    define'DATE_DIFF_WEEKS'  );
        
    define'DATE_DIFF_DAYS'   );
        
    define'DATE_DIFF_HOURS'  );
        
    define'DATE_DIFF_MINUTES');
        
    define'DATE_DIFF_SECONDS');
        function 
    date_diff$currTime$origTime$limit DATE_DIFF_SECONDS )
        {
            
    $currTime intval$currTime );
            
    $origTime intval$origTime );
            
    $limit    intval$limit );
            if ( 
    $currTime $origTime )
            {
                
    $diff $origTime $currTime;
            }
            else
            {
                
    $diff $currTime $origTime;
            }
            
    $showDiff '';
            
    // Years
            
    if ( $limit >= DATE_DIFF_YEARS )
            {
                
    $yrs floor$diff 31556926 ); // 31556926 secs/yr
                
    if ( $yrs )
                {
                    
    $diff      $diff - ( $yrs 31556926 );
                    
    $showDiff .= $yrs ' year';
                    
    $showDiff .= $yrs 's' '';
                }
                unset( 
    $yrs );
            }
            
    // Months
            
    if ( $limit >= DATE_DIFF_MONTHS )
            {
                
    $mos floor$difference 2629743.83 ); // 2629743.83 secs/mo
                
    if ( $mos )
                {
                    
    $diff      $diff - ( $mos 2629743.83 );
                    
    $showDiff .= empty( $showDiff ) ? '' ', ';
                    
    $showDiff .= $mos ' month'; ? 's' '';
                }
                unset( 
    $mos );
            }
            
    // Weeks
            
    if ( $limit >= DATE_DIFF_WEEKS )
            {
                
    $wks floor$diff 604800 ); // 604800 secs/wk
                
    if ( $wks )
                {
                    
    $diff      $diff - ( $wks 604800 );
                    
    $showDiff .= empty( $showDiff ) ? '' ', ';
                    
    $showDiff .= $wks ' week';
                    
    $showDiff .= $wks 's' '';
                }
                unset( 
    $wks );
            }
            
    // Days
            
    if ( $limit >= DATE_DIFF_DAYS )
            {
                
    $days floor$diff 86400 ); // 86400 secs/day
                
    if ( $days )
                {
                    
    $diff      $diff - ( $days 86400 );
                    
    $showDiff .= empty( $showDiff ) ? '' ', ';
                    
    $showDiff .= $days ' day';
                    
    $showDiff .= $days 's' '';
                }
                unset( 
    $days );
            }
            
    // Hours
            
    if ( $limit >= DATE_DIFF_HOURS )
            {
                
    $hrs floor$diff 3600 ); // 3600 secs/hr
                
    if ( $hrs )
                {
                    
    $diff      $diff - ( $hrs 3600 );
                    
    $showDiff .= empty( $showDiff ) ? '' ', ';
                    
    $showDiff .= $hrs ' hour';
                    
    $showDiff .= $hrs 's' '';
                }
                unset( 
    $hrs );
            }
            
    // Minutes
            
    if ( $limit >= DATE_DIFF_MINUTES )
            {
                
    $mins floor$diff 60 ); // 60 secs/min
                
    if ( $mins )
                {
                    
    $diff      $diff - ( $mins 60 );
                    
    $showDiff .= empty( $showDiff ) ? '' ', ';
                    
    $showDiff .= $mins ' minute';
                    
    $showDiff .= $mins 's' '';
                }
                unset( 
    $mins );
            }
            
    // Seconds
            
    if ( $limit >= DATE_DIFF_SECONDS )
            {
                if ( 
    $diff )
                {
                    
    $showDiff .= empty( $showDiff ) ? '' ', ';
                    
    $showDiff .= $diff ' second';
                    
    $showDiff .= $diff 's' '';
                }
            }
            unset( 
    $diff );
            
    // Return difference
            
    if ( $currTime $origTime )
            {
                
    $showDiff '- ' $showDiff;
            }
            return 
    $showDiff;
        }

    Last edited by kbluhm; 11-21-2007 at 02:37 PM.

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Well, I was changing some things around on my own site and noticed a typo (stray semi-colon) down near the $showDiff .= $mos . ' month' ? 's' : ''; line:
    PHP Code:
    /**
     * date_diff()
     * Calculates the difference between two unix timestamps
     *
     * @param    int      $currTime     UNIX timestamp
     * @param    int      $origTime     UNIX timestamp to compare
     * @param    const    $limit        How deep to drill down
     * @return   string                 The difference in time
     */
    if ( !function_exists'date_diff' ) )
    {
        
    define'DATE_DIFF_YEARS'  );
        
    define'DATE_DIFF_MONTHS' );
        
    define'DATE_DIFF_WEEKS'  );
        
    define'DATE_DIFF_DAYS'   );
        
    define'DATE_DIFF_HOURS'  );
        
    define'DATE_DIFF_MINUTES');
        
    define'DATE_DIFF_SECONDS');
        function 
    date_diff$currTime$origTime$limit DATE_DIFF_SECONDS )
        {
            
    $currTime intval$currTime );
            
    $origTime intval$origTime );
            
    $limit    intval$limit );
            if ( 
    $currTime $origTime )
            {
                
    $diff $origTime $currTime;
            }
            else
            {
                
    $diff $currTime $origTime;
            }
            
    $showDiff '';
            
    // Years
            
    if ( $limit >= DATE_DIFF_YEARS )
            {
                
    $yrs floor$diff 31556926 ); // 31556926 secs/yr
                
    if ( $yrs )
                {
                    
    $diff      $diff - ( $yrs 31556926 );
                    
    $showDiff .= $yrs ' year';
                    
    $showDiff .= $yrs 's' '';
                }
                unset( 
    $yrs );
            }
            
    // Months
            
    if ( $limit >= DATE_DIFF_MONTHS )
            {
                
    $mos floor$difference 2629743.83 ); // 2629743.83 secs/mo
                
    if ( $mos )
                {
                    
    $diff      $diff - ( $mos 2629743.83 );
                    
    $showDiff .= empty( $showDiff ) ? '' ', ';
                    
    $showDiff .= $mos ' month' 's' '';
                }
                unset( 
    $mos );
            }
            
    // Weeks
            
    if ( $limit >= DATE_DIFF_WEEKS )
            {
                
    $wks floor$diff 604800 ); // 604800 secs/wk
                
    if ( $wks )
                {
                    
    $diff      $diff - ( $wks 604800 );
                    
    $showDiff .= empty( $showDiff ) ? '' ', ';
                    
    $showDiff .= $wks ' week';
                    
    $showDiff .= $wks 's' '';
                }
                unset( 
    $wks );
            }
            
    // Days
            
    if ( $limit >= DATE_DIFF_DAYS )
            {
                
    $days floor$diff 86400 ); // 86400 secs/day
                
    if ( $days )
                {
                    
    $diff      $diff - ( $days 86400 );
                    
    $showDiff .= empty( $showDiff ) ? '' ', ';
                    
    $showDiff .= $days ' day';
                    
    $showDiff .= $days 's' '';
                }
                unset( 
    $days );
            }
            
    // Hours
            
    if ( $limit >= DATE_DIFF_HOURS )
            {
                
    $hrs floor$diff 3600 ); // 3600 secs/hr
                
    if ( $hrs )
                {
                    
    $diff      $diff - ( $hrs 3600 );
                    
    $showDiff .= empty( $showDiff ) ? '' ', ';
                    
    $showDiff .= $hrs ' hour';
                    
    $showDiff .= $hrs 's' '';
                }
                unset( 
    $hrs );
            }
            
    // Minutes
            
    if ( $limit >= DATE_DIFF_MINUTES )
            {
                
    $mins floor$diff 60 ); // 60 secs/min
                
    if ( $mins )
                {
                    
    $diff      $diff - ( $mins 60 );
                    
    $showDiff .= empty( $showDiff ) ? '' ', ';
                    
    $showDiff .= $mins ' minute';
                    
    $showDiff .= $mins 's' '';
                }
                unset( 
    $mins );
            }
            
    // Seconds
            
    if ( $limit >= DATE_DIFF_SECONDS )
            {
                if ( 
    $diff )
                {
                    
    $showDiff .= empty( $showDiff ) ? '' ', ';
                    
    $showDiff .= $diff ' second';
                    
    $showDiff .= $diff 's' '';
                }
            }
            unset( 
    $diff );
            
    // Return difference
            
    if ( $currTime $origTime )
            {
                
    $showDiff '- ' $showDiff;
            }
            return 
    $showDiff;
        }



  •  

    Posting Permissions

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