Current location: Hot Scripts Forums » Programming Languages » PHP » It is possible to get difference minutes from date and time diffrence


It is possible to get difference minutes from date and time diffrence

Reply
  #1 (permalink)  
Old 01-21-10, 12:37 AM
anacy_nivas's Avatar
anacy_nivas anacy_nivas is offline
Newbie Coder
 
Join Date: Sep 2008
Location: coimbatore
Posts: 47
Thanks: 11
Thanked 0 Times in 0 Posts
It is possible to get difference minutes from date and time diffrence

I did following coding to display date and time difference between two dates( $current_date, $data_ref). Date format is date("Y-m-d h-i-s").

$current_year = substr($current_date,0,4);
$current_month = substr($current_date,5,2);
$current_day = substr($current_date,8,2);
$current_hour = substr($current_date,11,2);
$current_min = substr($current_date,14,2);
$ref_year = substr($data_ref,0,4);
$ref_month = substr($data_ref,5,2);
$ref_day = substr($data_ref,8,2);
$ref_hour = substr($data_ref,11,2);
$ref_min = substr($data_ref,14,2);
$minutes_Diff = $ref_min-$current_min;
$hour_Diff = $ref_hour-$current_hour;
$day_Diff = $ref_day-$current_day;
$month_Diff = $ref_month-$current_month;
$year_Diff = $ref_year-$current_year;

Finally i get the date difference , Hour difference .
It is possible to calculate total minutes difference ?
Reply With Quote
  #2 (permalink)  
Old 01-21-10, 06:35 AM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Lots of ways to do this....

PHP Code:

$to_time=strtotime($current_date);
$from_time=strtotime($data_ref);
echo 
round(abs($to_time $from_time) / 60,2)." minute"

You could use this, although you'll need to convert your dates to mySQL timestamps:

PHP Code:

function totalTimeDiffMins($start$end){
// returns difference between two 
// mysql timestamps as minutes

    
$unix_timestamp1 strtotime($end);
    
$unix_timestamp2 strtotime($start);

    
// get difference between the two in seconds
    
$time_period = ($unix_timestamp1 $unix_timestamp2);
    
    if(
$time_period 0){
        
$time_in_mins = ($time_period 60);
        return 
$time_in_mins;
    }else{
        return 
"DATE ERROR";
    }

__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Reply With Quote
  #3 (permalink)  
Old 01-21-10, 03:57 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
PHP Code:

$current_date date("Y-m-d-h-i-s");

$date explode('-',$current_date);

$year $date[0];
$month $date[1];
$day $date[2];
$hour $date[3];
$minute $date[4];
$second $date[5]; 
Reply With Quote
The Following User Says Thank You to Jcbones For This Useful Post:
anacy_nivas (01-22-10)
  #4 (permalink)  
Old 01-21-10, 08:40 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
I think he wanted to get the difference in seconds between two dates, unless I misread his post.

Quote:
Originally Posted by Jcbones View Post
PHP Code:

$current_date date("Y-m-d-h-i-s");


$date explode('-',$current_date);

$year $date[0];
$month $date[1];
$day $date[2];
$hour $date[3];
$minute $date[4];
$second $date[5]; 
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Reply With Quote
The Following User Says Thank You to End User For This Useful Post:
anacy_nivas (01-22-10)
  #5 (permalink)  
Old 01-22-10, 04:29 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
I know, I was just showing an easier way to strip the time using explode(), rather than substr().

But, since you said something, I went ahead and created a function that may be useful.

Returns time difference from two timestamps. User decides calculation limits. I could take this to Years, but right now it is limited to Days and below.

PHP Code:

<?php

//set real dates for start and end, otherwise *nix the strtotime() lines.
//$return 'days' will return days/hours/minutes/seconds.
//$return 'hours' will return hours/minutes/seconds.
//$return 'minutes' will return minutes/seconds.
//$return 'seconds' will return seconds.
function timeDifference($start,$end,$return='days') {
    
//change times to Unix timestamp.
    
$start strtotime($start);
    
$end strtotime($end);
        if(
$start $end) {
            return 
'Please make sure the start date is less than the end date.';
        }
    
//subtract dates
    
$difference $end $start;
    
$time NULL;
    
//calculate time difference.
    
switch($return) {
        case 
'days':
             
$days floor($difference/86400);
                
$difference $difference 86400;
                    
$time .= $days ' Days, ';
        case 
'hours':
            
$hours floor($difference/3600);
                
$difference $difference 3600;
                    
$time .= $hours ' Hours, ';
        case 
'minutes':
            
$minutes floor($difference/60);
                
$difference $difference 60;
                    
$time .= $minutes ' Minutes, ';
        case 
'seconds':
            
$seconds $difference;
                
$time .= $seconds ' Seconds';
    }
    
    return 
$time;   
    
}

echo 
timeDifference('2008-06-13 14:39:28','2008-07-14 19:25:07','seconds');

?>
Reply With Quote
  #6 (permalink)  
Old 01-22-10, 07:34 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
Just for fun ...

PHP Code:

$t=max($start,$end)-min($start,$end); 

... because that way, even if the user messes up the dates, the code works.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Display local date & time outhowz42 PHP 3 04-28-09 12:35 PM
date / time validation mcrob PHP 13 04-23-08 09:06 PM
having Loop issues, help... Advanced todayscoffee PHP 2 02-27-06 12:36 AM
inserting and retrieving date and time from mysql database stealth04 PHP 5 07-29-04 11:19 AM
help regarding insertion of date time wajeeh_r ASP 1 03-04-04 04:05 PM


All times are GMT -5. The time now is 08:06 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.