/    Sign up×
Community /Pin to ProfileBookmark

Want to convert DateTime difference result to Minutes

Hi Friends,

I’m struck up in converting the DateTime difference result as Minutes, could you please anyone help me on this?

Here am explaining my trouble:
I’m fetching start and end time from MySQL (saved in the form of datetime type; e.g $start = ‘2010-10-22 11:15:09’; $end = ‘2010-10-23 19:08:16’?
using the below function I got the difference perfectly!
like : Array ( [days] => 1 [hours] => 7 [minutes] => 53 [seconds] => 7 )

I want to have the complete datetime difference in the form of minutes?

[QUOTE]

<?php
function get_time_difference( $start, $end ){
$uts[‘start’] = strtotime( $start );
$uts[‘end’] = strtotime( $end );
if( $uts[‘start’]!==-1 && $uts[‘end’]!==-1 )
{
if( $uts[‘end’] >= $uts[‘start’] )
{
$diff = $uts[‘end’] – $uts[‘start’];
if( $days=intval((floor($diff/86400))) )
$diff = $diff &#37; 86400;
if( $hours=intval((floor($diff/3600))) )
$diff = $diff % 3600;
if( $minutes=intval((floor($diff/60))) )
$diff = $diff % 60;
$diff = intval( $diff );

return( array(‘days’=>$days, ‘hours’=>$hours, ‘minutes’=>$minutes, ‘seconds’=>$diff) );
}
else
{
trigger_error( “Ending date/time is earlier than the start date/time”, E_USER_WARNING );
}
}
else
{
trigger_error( “Invalid date/time data detected”, E_USER_WARNING );
}
return( false );
}

$start = ‘2010-10-22 11:15:09’;
$end = ‘2010-10-23 19:08:16’;

$diff = get_time_difference($start,$end);
print_r ($diff);
// echo http_build_query($diff);
?>

[/QUOTE]

Regards,
Thirilog

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@zimonyiOct 25.2010 — I suggest you check out the help for the date_diff() function and go from there.

I think you already have a method that does that in the PHP language, no need to create it yourself.

From the PHP manual on DateTime object:

[CODE]
<?php
$datetime1 = date_create('2009-10-11');
$datetime2 = date_create('2009-10-13');
$interval = date_diff($datetime1, $datetime2);
echo $interval->format('&#37;R%d days');
?>
[/CODE]


You should only have to change the "%R%d" to whatever you want it to display (example minutes).

Archie
Copy linkTweet thisAlerts:
@ThirilogauthorOct 25.2010 — Many Thanks!

To be the temp solution i have followed the below steps, however i'll try ?

$day = $diff['days'];

$dayc = (24*$day)*60;

$hrs = $diff['hours'];

$hrsc = $hrs*60;

$mins = $diff['minutes'];

$sec = $diff['seconds'];

$secc = $sec/60;

$Tmins = $dayc+$hrsc+$mins+$secc;
[/QUOTE]

Regards,

Thirilog
Copy linkTweet thisAlerts:
@IBM701Apr 10.2013 — [code=php]
$difference = $datetime1->diff($datetime2);
echo $difference->format("%i%a minutes");
[/code]
×

Success!

Help @Thirilog spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...