/    Sign up×
Community /Pin to ProfileBookmark

Issue in getting the time difference between two variables

I have two textboxes that inputted time, and I want to get the difference between two time.

for example:

$IN = 13:35;
$OUT = 17:35;

$OTHours = ($OUT – $IN);

$OTHours = 4.00;

and it is correct, but I have a scenario like this:

$IN = 21:35;
$OUT = 05:35;

$OTHours = -16.00;

it should be 8.00.

Any help is highly appreciated.

Thank you..

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@ssystemsJan 30.2012 — Would this work for you?

[code=php]
$reference = '2012-01-01';
$start = strtotime($reference . ' ' . $in);
$end = strtotime($reference . ' ' . $out);
$difference = date('H:i', $end-$start);
[/code]
Copy linkTweet thisAlerts:
@NogDogJan 30.2012 — If using a reasonably current version of PHP (>= 5.3.0), you can use [url=http://www.php.net/manual/en/datetime.diff.php]Datetime::diff()[/url] to handle this. If not, you'll probably want to convert each time to a UNIX timestamp integer via strtotime(), get the difference (which will be in seconds), then divide by 3600 to get the hours.
Copy linkTweet thisAlerts:
@newphpcoderauthorJan 30.2012 — Hi...

Now, I think i need to put also the date like:

$IN = '2012-01-13 21:35'

$OUT = '2012-01-14 05:35'

Can you give me example to get the time difference using strtotime?

Thank you
Copy linkTweet thisAlerts:
@ssystemsJan 30.2012 — I thought I just gave it to you.

[CODE]

$IN = '2012-01-13 21:35'
$OUT = '2012-01-14 05:35'

$start = strtotime($IN);
$end = strtotime($OUT );
$difference = $end - $start
$hour_difference = $difference / 3600;
[/CODE]


did it a line a time so you can follow.
Copy linkTweet thisAlerts:
@newphpcoderauthorFeb 06.2012 — I tried that when i got this data:

IN 2011-12-22 13:30

OUT 2011-12-22 15:00

the OTHours become 1.00 it should be 1.30

by the way the OT Hours field is decimal type.

Thank you
Copy linkTweet thisAlerts:
@eval_BadCode_Feb 06.2012 — [CODE]
php > $startDate = DateTime::createFromFormat("G:i", '13:35');
php > $endDate = DateTime::createFromFormat("G:i", '17:35');
php > echo $startDate->diff($endDate)->format('%h');
4
[/CODE]


now lets try it against this ...

$IN = 21:35;

$OUT = 05:35;

god help us

[CODE]
php > $startDate = DateTime::createFromFormat("G:i", '21:35');
php > $endDate = DateTime::createFromFormat("G:i", '05:35');
php > echo $startDate->diff($endDate)->format('%h');
16
[/CODE]


As I suspected, providing only hour and minute is a very bad idea. But if you can provide the date part of "Date""Time", then you can get this one easily.

[CODE]
new2net@crosstalk~$ php -a
Interactive shell

php > $startDate = DateTime::createFromFormat('Y-m-d H:i:s', '2012-02-05 21:35:00');
php > $endDate = DateTime::createFromFormat('Y-m-d H:i:s', '2012-02-06 05:35:00');
php > echo (int) $startDate->diff($endDate,true)->format('%h');
8
[/CODE]


Anyone else see the three problems with this one?

[CODE]
php > if(!$startDate || !$endDate) throw new Exception('The dates were not properly parsed!');
[/CODE]


Now, time-zone is an issue. Maybe DateTime::RFC850 is a better format to use.

The last issue is daylight savings time, which to be honest I can hardly even solve for myself. It completely thrashed a project of mine because my client told me to not worry about daylight savings time, which is like "perfect", but it completely showed up and I had to recode and retest multiple parts of the project which was a huge setback and made the $timeframeForTheProject->add(new DateInterval('P1D')); ? I got compensated for it though.

That is why I mention it ahead of time, [B][COLOR="DarkRed"]you MUST consider your DateTime format before you crank out a bunch of code[/COLOR][/B]. If daylight savings time is going to be an issue then you will need to pick a datetime format that understands what daylight savings time is!!! You can not ignore this if you are automating payments, you can end up losing a lot of money or possibly making a lot of people mad by not paying them enough.

Edit:

Also, why are you storing minutes if you're going to truncate them. Pay them by the minute, not hour. I would be outraged if I got paid for 1 hour even though I worked 1 hour and 30 minutes. A worker might have kids to feed, just saying ?
[CODE]
php > $startDate = DateTime::createFromFormat('Y-m-d H:i:s', '2012-02-05 21:55:00');
php > $endDate = DateTime::createFromFormat('Y-m-d H:i:s', '2012-02-06 05:35:00');
php > printf('%.2f', $startDate->diff($endDate,true)->format('%h')+$startDate->diff($endDate,true)->format('%i')/60);
7.67
[/CODE]


'Y-m-d H:i:s T +I' looks solid. Or you can just go with epoch time, which is easier to understand.

Cheers
×

Success!

Help @newphpcoder 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 6.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...