PDA

View Full Version : time conversion

dizyn
11-21-2007, 10:51 AM
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

dizyn
11-21-2007, 11:38 AM
I have solved the problem
here is solution

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

kbluhm
11-21-2007, 01:55 PM
Here is something I wrote a while back that should do just what you'd like.
/**
* 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' , 1 );
define( 'DATE_DIFF_MONTHS' , 2 );
define( 'DATE_DIFF_WEEKS' , 3 );
define( 'DATE_DIFF_DAYS' , 4 );
define( 'DATE_DIFF_HOURS' , 5 );
define( 'DATE_DIFF_MINUTES', 6 );
define( 'DATE_DIFF_SECONDS', 7 );
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 > 0 )
{
\$diff = \$diff - ( \$yrs * 31556926 );
\$showDiff .= \$yrs . ' year';
\$showDiff .= \$yrs > 1 ? 's' : '';
}
unset( \$yrs );
}
// Months
if ( \$limit >= DATE_DIFF_MONTHS )
{
\$mos = floor( \$difference / 2629743.83 ); // 2629743.83 secs/mo
if ( \$mos > 0 )
{
\$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 > 0 )
{
\$diff = \$diff - ( \$wks * 604800 );
\$showDiff .= empty( \$showDiff ) ? '' : ', ';
\$showDiff .= \$wks . ' week';
\$showDiff .= \$wks > 1 ? 's' : '';
}
unset( \$wks );
}
// Days
if ( \$limit >= DATE_DIFF_DAYS )
{
\$days = floor( \$diff / 86400 ); // 86400 secs/day
if ( \$days > 0 )
{
\$diff = \$diff - ( \$days * 86400 );
\$showDiff .= empty( \$showDiff ) ? '' : ', ';
\$showDiff .= \$days . ' day';
\$showDiff .= \$days > 1 ? 's' : '';
}
unset( \$days );
}
// Hours
if ( \$limit >= DATE_DIFF_HOURS )
{
\$hrs = floor( \$diff / 3600 ); // 3600 secs/hr
if ( \$hrs > 0 )
{
\$diff = \$diff - ( \$hrs * 3600 );
\$showDiff .= empty( \$showDiff ) ? '' : ', ';
\$showDiff .= \$hrs . ' hour';
\$showDiff .= \$hrs > 1 ? 's' : '';
}
unset( \$hrs );
}
// Minutes
if ( \$limit >= DATE_DIFF_MINUTES )
{
\$mins = floor( \$diff / 60 ); // 60 secs/min
if ( \$mins > 0 )
{
\$diff = \$diff - ( \$mins * 60 );
\$showDiff .= empty( \$showDiff ) ? '' : ', ';
\$showDiff .= \$mins . ' minute';
\$showDiff .= \$mins > 1 ? 's' : '';
}
unset( \$mins );
}
// Seconds
if ( \$limit >= DATE_DIFF_SECONDS )
{
if ( \$diff > 0 )
{
\$showDiff .= empty( \$showDiff ) ? '' : ', ';
\$showDiff .= \$diff . ' second';
\$showDiff .= \$diff > 1 ? 's' : '';
}
}
unset( \$diff );
// Return difference
if ( \$currTime < \$origTime )
{
\$showDiff = '- ' . \$showDiff;
}
return \$showDiff;
}
}

kbluhm
11-23-2007, 09:30 PM
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:

/**
* 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' , 1 );
define( 'DATE_DIFF_MONTHS' , 2 );
define( 'DATE_DIFF_WEEKS' , 3 );
define( 'DATE_DIFF_DAYS' , 4 );
define( 'DATE_DIFF_HOURS' , 5 );
define( 'DATE_DIFF_MINUTES', 6 );
define( 'DATE_DIFF_SECONDS', 7 );
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 > 0 )
{
\$diff = \$diff - ( \$yrs * 31556926 );
\$showDiff .= \$yrs . ' year';
\$showDiff .= \$yrs > 1 ? 's' : '';
}
unset( \$yrs );
}
// Months
if ( \$limit >= DATE_DIFF_MONTHS )
{
\$mos = floor( \$difference / 2629743.83 ); // 2629743.83 secs/mo
if ( \$mos > 0 )
{
\$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 > 0 )
{
\$diff = \$diff - ( \$wks * 604800 );
\$showDiff .= empty( \$showDiff ) ? '' : ', ';
\$showDiff .= \$wks . ' week';
\$showDiff .= \$wks > 1 ? 's' : '';
}
unset( \$wks );
}
// Days
if ( \$limit >= DATE_DIFF_DAYS )
{
\$days = floor( \$diff / 86400 ); // 86400 secs/day
if ( \$days > 0 )
{
\$diff = \$diff - ( \$days * 86400 );
\$showDiff .= empty( \$showDiff ) ? '' : ', ';
\$showDiff .= \$days . ' day';
\$showDiff .= \$days > 1 ? 's' : '';
}
unset( \$days );
}
// Hours
if ( \$limit >= DATE_DIFF_HOURS )
{
\$hrs = floor( \$diff / 3600 ); // 3600 secs/hr
if ( \$hrs > 0 )
{
\$diff = \$diff - ( \$hrs * 3600 );
\$showDiff .= empty( \$showDiff ) ? '' : ', ';
\$showDiff .= \$hrs . ' hour';
\$showDiff .= \$hrs > 1 ? 's' : '';
}
unset( \$hrs );
}
// Minutes
if ( \$limit >= DATE_DIFF_MINUTES )
{
\$mins = floor( \$diff / 60 ); // 60 secs/min
if ( \$mins > 0 )
{
\$diff = \$diff - ( \$mins * 60 );
\$showDiff .= empty( \$showDiff ) ? '' : ', ';
\$showDiff .= \$mins . ' minute';
\$showDiff .= \$mins > 1 ? 's' : '';
}
unset( \$mins );
}
// Seconds
if ( \$limit >= DATE_DIFF_SECONDS )
{
if ( \$diff > 0 )
{
\$showDiff .= empty( \$showDiff ) ? '' : ', ';
\$showDiff .= \$diff . ' second';
\$showDiff .= \$diff > 1 ? 's' : '';
}
}
unset( \$diff );
// Return difference
if ( \$currTime < \$origTime )
{
\$showDiff = '- ' . \$showDiff;
}
return \$showDiff;
}
}