php time question
Moderator: General Moderators
php time question
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?
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?
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: php time question
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.
Re: php time question
So would I use like a date function to get the date?
Or would I do that with unix time?
Code: Select all
date("H:i A")
- AbraCadaver
- DevNet Master
- Posts: 2572
- Joined: Mon Feb 24, 2003 10:12 am
- Location: The Republic of Texas
- Contact:
Re: php time question
I don't know what you're asking. You asked for the hour and minute difference between two times.jefffan24 wrote:So would I use like a date function to get the date?Or would I do that with unix time?Code: Select all
date("H:i A")
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.
Re: php time question
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.
Re: php time question
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.
Re: php time question
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.
Thanks for your replies I'll try some of the stuff posted here and see if I can get any of that to work.
Re: php time question
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
http://dev.mysql.com/doc/refman/5.1/en/ ... n_timediff
Re: php time question
That is definitely interesting, I didn't know mysql could do that. I think that would the most efficient way to do this.