Septanet Forum  

Go Back   Septanet Forum > Computers, Software, Harware, Information Technology > PHP Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 23-08-2011, 10:33 AM   #1
Senior Member
 
sharma.s.prakash's Avatar
 
Join Date: Aug 2011
Posts: 110
Smile Date and Time difference

/**
* To fetch the difference between two dates
* @param date $firstDate
* @param date $secondDate
*/


PHP Code:
function dateDiffSQL($firstDate$secondDate) {
    
$conn=mysqli_connect($DBSERVER$DBUSER$DBPASSWORD$DBNAME) or die('can not connect');
    
$qryDiff "SELECT DATEDIFF('$secondDate', '$firstDate') as diff";
    
$rsDiff mysqli_query($conn$qryDiff);
    
$rowDiff mysqli_fetch_assoc($rsDiff);
    return 
$rowDiff[diff];

/**
* To fetch the time elapsed between the two dates
* @param datetime $startDtTime
* @param datetime $endDtTime
*/

PHP Code:
function timeDiffSql($startDtTime$endDtTime) {
    
$conn=mysqli_connect($DBSERVER$DBUSER$DBPASSWORD$DBNAME) or die('can not connect');
    
$qryTimeDiff="SELECT TIMEDIFF('$endDtTime', '$startDtTime') as hrs_taken";
    
$rsTimeDiff=mysqli_query($conn,$qryTimeDiff);
    
$rowTimeDiff=mysqli_fetch_assoc($rsTimeDiff);
    
$hrsTaken=$rowTimeDiff[hrs_taken];
    return 
$hrsTaken;

sharma.s.prakash is offline  
Digg this Post!Bookmark Post in Technorati
Reply With Quote
Old 23-08-2011, 02:20 PM   #2
Senior Member
 
kaman's Avatar
 
Join Date: Aug 2011
Posts: 111
Thumbs up

Very useful! Thx for sharing.
kaman is offline  
Digg this Post!Bookmark Post in Technorati
Reply With Quote
Old 24-08-2011, 10:08 AM   #3
Senior Member
 
sharma.s.prakash's Avatar
 
Join Date: Aug 2011
Posts: 110
Smile

thanx for your prompt response
sharma.s.prakash is offline  
Digg this Post!Bookmark Post in Technorati
Reply With Quote
Old 07-09-2011, 02:51 PM   #4
Junior Member
 
Join Date: Sep 2011
Posts: 1
Default No DB Access

Hi I dont want to handle it through DB.. any other way??

AI
amit is offline  
Digg this Post!Bookmark Post in Technorati
Reply With Quote
Old 08-09-2011, 09:22 AM   #5
Senior Member
 
sharma.s.prakash's Avatar
 
Join Date: Aug 2011
Posts: 110
Smile

Try This

PHP Code:
$varDate1='1980-01-27';
$varDate2='2011-06-07';
echo 
funcDateDiff($varDate1,$varDate2);

function 
funcDateDiff($varDate1,$varDate2) {
    if(
$varDate1<=$varDate2) {
        
$arrDate1=explode('-',$varDate1);
        
$arrDate2=explode('-',$varDate2);
        
$arrDateDiff=array();
        if(
$arrDate2[2]<$arrDate1[2]) {
            
$arrDateDiff[2]=$arrDate2[2]+30-$arrDate1[2];
            
$arrDate2[1]=$arrDate2[1]-1;
        } else {
            
$arrDateDiff[2]=$arrDate2[2]-$arrDate1[2];
        }
        if(
$arrDate2[1]<$arrDate1[1]) {
            
$arrDateDiff[1]=$arrDate2[1]+12-$arrDate1[1];
            
$arrDate2[1]=$arrDate2[1]-1;
        } else {
            
$arrDateDiff[1]=$arrDate2[1]-$arrDate1[1];
        }
        
$arrDateDiff[0]=$arrDate2[0]-$arrDate1[0];
        
$varDateDiff=$arrDateDiff[0].' Year '.$arrDateDiff[1].' Month '.$arrDateDiff[2].' Days ';
    } else {
        
$varDateDiff='invalid date';
    }
    return 
$varDateDiff;

Output
Code:
        31 Year 4 Month 10 Days
sharma.s.prakash is offline  
Digg this Post!Bookmark Post in Technorati
Reply With Quote
Old 08-09-2011, 09:24 AM   #6
Senior Member
 
sharma.s.prakash's Avatar
 
Join Date: Aug 2011
Posts: 110
Smile

Well come on septanet
sharma.s.prakash is offline  
Digg this Post!Bookmark Post in Technorati
Reply With Quote
Old 08-09-2011, 09:49 AM   #7
Senior Member
 
sachin's Avatar
 
Join Date: Aug 2011
Posts: 131
Default

Excellent Satya Prakash

and

Welcome Amit
sachin is offline  
Digg this Post!Bookmark Post in Technorati
Reply With Quote
Reply

Tags
date difference, time difference

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +5.5. The time now is 12:54 PM.


Content Relevant URLs by vBSEO 3.5.0 RC2