473,480 Members | 1,781 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how to find the difference between two times in PHP?

127 New Member
i have stored the user break times in the database.

break out time and break in time... now i have to find the break time.

that is ( break in time - break out time).

break out time = 2010-10-22 10:27:10
break in time = 2010-10-22 10:50:10
these values are stored in the db.
so the break time is 23 mins...

how i find this? plz help...
Oct 22 '10 #1
3 11220
code green
1,726 Recognized Expert Top Contributor
In PHP convert it to timestamp using strtotime, subtract and the answer will be in seconds.
Then divide by the units required, i.e. 1 day = 86400 secs

In SQL use DATEDIFF or DATEADD with negative values
Oct 22 '10 #2
impin
127 New Member
thanks. i find the ansewer...

using this function.

Expand|Select|Wrap|Line Numbers
  1.  function dateDiff($time1, $time2, $precision = 6) {
  2.  
  3.     if (!is_int($time1)) {
  4.       $time1 = strtotime($time1);
  5.     }
  6.     if (!is_int($time2)) {
  7.       $time2 = strtotime($time2);
  8.     }
  9.  
  10.  
  11.     if ($time1 > $time2) {
  12.       $ttime = $time1;
  13.       $time1 = $time2;
  14.       $time2 = $ttime;
  15.     }
  16.  
  17.  
  18.     $intervals = array('year','month','day','hour','minute','second');
  19.     $diffs = array();
  20.  
  21.  
  22.     foreach ($intervals as $interval) {
  23.  
  24.       $diffs[$interval] = 0;
  25.  
  26.       $ttime = strtotime("+1 " . $interval, $time1);
  27.  
  28.       while ($time2 >= $ttime) {
  29.     $time1 = $ttime;
  30.     $diffs[$interval]++;
  31.  
  32.     $ttime = strtotime("+1 " . $interval, $time1);
  33.       }
  34.     }
  35.  
  36.     $count = 0;
  37.     $times = array();
  38.  
  39.     foreach ($diffs as $interval => $value) {
  40.  
  41.       if ($count >= $precision) {
  42.     break;
  43.       }
  44.  
  45.       if ($value > 0) {
  46.  
  47.     if ($value != 1) {
  48.       $interval .= "s";
  49.     }
  50.  
  51.     $times[] = $value . " " . $interval;
  52.     $count++;
  53.       }
  54.     }
  55.  
  56.  
  57.     return implode(", ", $times);
  58.   }
  59.  
Expand|Select|Wrap|Line Numbers
  1. $break= dateDiff("$breakin", "$breakout") . "\n";
  2.  

now i want to add the times intervals...

they user take several breaks in single day. i want to add all the break duration. so i can get the total break time for the user in the whole day...

how to do it?

users first break time duration 30 mins.
second break duration 25 mins
third break duration 20 mins

i am getting like this...

now i want to add 3 break duartions. so the users total break time is 1 hour and 15 mins...

how to do it?
Oct 23 '10 #3
charles07
45 New Member
impin you can easily do it in mysql

SELECT TIMEDIFF('2010-10-22 10:50:10','2010-10-22 10:27:10') AS timedifference
this would give the difference as 00:23:00

i believe break in time & break out time are stored in DB
Jan 13 '12 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1593
by: AzGhanv/. | last post by:
Hi, I m searching for some Script / Function ... to find difference in data b/w 2 similar tables (exactly same fields structure) in sql 2000. plz update me asap ! thanks in advance !
1
21351
by: bradleyc | last post by:
How would you calculate the difference between two dates?
1
3260
by: Prafull Soni | last post by:
hi Can anyone give me program to find difference between two dates? thanks Prafull Soni
5
19383
by: Ali Baba | last post by:
Hi, Is there is equivalent of VB's DateDiff() method in C#. I need to find difference in months between two dates that are years apart. Docs says that I can use TimeSpan like: TimeSpam ts =...
23
13994
by: thebjorn | last post by:
For the purpose of finding someone's age I was looking for a way to find how the difference in years between two dates, so I could do something like: age = (date.today() - born).year but that...
5
6381
by: Konstantinos Pachopoulos | last post by:
Hi, does any body now any such algorith? to find difference in days from YYYYMMDD to YYYYMMDD? Or just an algorithm, that converts YYYYMMDD to seconds since the epoch? Thanks
2
3172
by: manishamca | last post by:
can anyone say me how to find the difference of values present in two textbox and display the result in the third textbox. for eg: <input type=text name=t1 value=123> <input...
1
2603
rizwan6feb
by: rizwan6feb | last post by:
I am developing an application in VB.net 2005 that synchronizes two mysql databases of same structure. I have done most of the work but having problem in finding the difference in records in 2...
4
4765
dlite922
by: dlite922 | last post by:
Short Version How do you find the difference between two database with the same structure but different data? with SQL (results will be ingested with PHP). Long Version Seems like a little...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7048
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7088
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5342
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4783
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4485
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2997
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
183
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.