Current location: Hot Scripts Forums » Programming Languages » PHP » Calculate the difference between dates


Calculate the difference between dates

Reply
  #1 (permalink)  
Old 03-15-10, 12:04 PM
az88 az88 is offline
Newbie Coder
 
Join Date: Feb 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Spin Calculate the difference between dates

Say I have 2 dates stored in variables like so...

PHP Code:

$date $row['date'];

$now date("Y-m-d H:i:s"); 
How would I go about finding the difference between them like:

PHP Code:

echo $now $date
Thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 03-15-10, 03:31 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 03-16-10, 04:12 PM
az88 az88 is offline
Newbie Coder
 
Join Date: Feb 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
I'm not sure how I can achieve what I am trying to do using the strtotime() function
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 03-16-10, 06:04 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
strtotime has some built in calculations - where you can say $tomorrow=strtotime("+1 day");

However, if that's not suitable, you can use

PHP Code:

$date1=strtotime($date);

$date2=now();
$difference=$date2-$date1;
$aResult=strptime($difference);
var_dump($aResult); 
PHP: strptime - Manual

Not tested
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 03-17-10, 09:40 AM
az88 az88 is offline
Newbie Coder
 
Join Date: Feb 2010
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Ok there does seem to be a few bugs in that.

Fatal error: Call to undefined function now() in ...

Warning: strptime() expects exactly 2 parameters, 1 given in

However I tweaked it a bit but I'm still not getting the result I am wanting.

PHP Code:

        $row mysql_fetch_array($result);

        
$date $row['date'];

        
$format '%d/%m/%Y %H:%M:%S';

        
$date1=strtotime($date);
        
$date2=date("Y-m-d H:i:s");
        
$difference=$date2-$date1;
        
$aResult=strptime($difference,$format);
        echo 
$aResult
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 03-17-10, 02:01 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
This $date2=date("Y-m-d H:i:s"); should probably be $date2=time();

As I said - I didn't test it.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 03-24-10, 05:27 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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
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
It is possible to get difference minutes from date and time diffrence anacy_nivas PHP 5 01-22-10 08:34 PM
Difference between two dates n3wb!e Script Requests 3 05-12-06 01:36 AM
MySQL Dates DAL PHP 5 04-22-06 08:00 AM
how to calculate sharad PHP 2 06-29-04 06:16 PM
How to find the difference between two dates and make multiple inserts based on it? wyseguy ASP 1 10-08-03 12:53 PM


All times are GMT -5. The time now is 01:52 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.