php time question

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jefffan24
Forum Commoner
Posts: 72
Joined: Mon Nov 02, 2009 8:18 am

php time question

Post by jefffan24 »

Ok so I have a question about the best way to do something.

I'm creating a time management sort of thing with databases and users and what not. But what I need help with is what is most efficient way to get the difference between 2 times?

So like

4:45 AM and 1:20 PM

So I know that the difference between the two is 8 hours and 35 minutes. But what kind of algorithm can I use with php to get that?

I was thinking using unix time but I couldn't get it to work right so any ideas?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: php time question

Post by AbraCadaver »

I don't know about best, but here is "a" way:

Code: Select all

$t1 = '4:45 AM';
$t2 = '1:20 PM';
 
$hours = floor(abs(strtotime($t1) - strtotime($t2)) / 3600);
$minutes = abs(strtotime($t1) - strtotime($t2)) % 3600 / 60;
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
jefffan24
Forum Commoner
Posts: 72
Joined: Mon Nov 02, 2009 8:18 am

Re: php time question

Post by jefffan24 »

So would I use like a date function to get the date?

Code: Select all

 
date("H:i A")
 
Or would I do that with unix time?
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: php time question

Post by AbraCadaver »

jefffan24 wrote:So would I use like a date function to get the date?

Code: Select all

 
date("H:i A")
 
Or would I do that with unix time?
I don't know what you're asking. You asked for the hour and minute difference between two times.

If you mean how to get $t1 and $t2 in the above example, then yes.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: php time question

Post by pickle »

I don't know of any bullet-proof way to do this. ~AbraCadaver method would work for most cases, except when the two times are on either side of a time change (to or from Daylight Saving Time). Then you'd be off an hour either direction.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: php time question

Post by JakeJ »

In the case of time zone differences, you'd have to pull the local time zone from the client computer and calculate against that. Probably base it all on GMT.
jefffan24
Forum Commoner
Posts: 72
Joined: Mon Nov 02, 2009 8:18 am

Re: php time question

Post by jefffan24 »

yeah I don't care about the actual time I just need to know the total time worked on a project (by the user pressing a start button setting a field and an end button setting another field). Then the code takes a difference of the 2 times to get the total time worked on the project.

Thanks for your replies I'll try some of the stuff posted here and see if I can get any of that to work.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: php time question

Post by Benjamin »

Since the dates are stored in the database, depending on your implementation the best solution may be to use MySQL's built in TIMEDIFF() function.

http://dev.mysql.com/doc/refman/5.1/en/ ... n_timediff
jefffan24
Forum Commoner
Posts: 72
Joined: Mon Nov 02, 2009 8:18 am

Re: php time question

Post by jefffan24 »

That is definitely interesting, I didn't know mysql could do that. I think that would the most efficient way to do this.
Post Reply