kernelgpf Posted October 4, 2007 Share Posted October 4, 2007 I recently switched this code to subtract days to hours, but it isn't working.. it's coming out with "343957987" hours and crazy stuff like that. Here's my code- $expirationdate[0] = $row3[datetillcompletion]; $startdate[0] = date("l, M dS (h:i a) "); $startdate = strtotime($startdate[0]); $expirationdate = strtotime($expirationdate[0]); $delta = $expirationdate - $startdate; $final=round($delta/3600); Quote Link to comment https://forums.phpfreaks.com/topic/71754-solved-help-with-finding-difference-in-hours-between-two-times/ Share on other sites More sharing options...
kernelgpf Posted October 6, 2007 Author Share Posted October 6, 2007 Bump. Quote Link to comment https://forums.phpfreaks.com/topic/71754-solved-help-with-finding-difference-in-hours-between-two-times/#findComment-363081 Share on other sites More sharing options...
kernelgpf Posted October 6, 2007 Author Share Posted October 6, 2007 The DB row "datetillcompletion" is a timestamp, and it's being inserted correctly. Quote Link to comment https://forums.phpfreaks.com/topic/71754-solved-help-with-finding-difference-in-hours-between-two-times/#findComment-363083 Share on other sites More sharing options...
heckenschutze Posted October 6, 2007 Share Posted October 6, 2007 $time1 = time(); $time2 = time() + 328523; /* some random number */ $dSecs = abs($time2 - $time1); $dHours = $dSecs / pow(60, 2); Something like that, there is probably a PHP function to do this... Quote Link to comment https://forums.phpfreaks.com/topic/71754-solved-help-with-finding-difference-in-hours-between-two-times/#findComment-363086 Share on other sites More sharing options...
kernelgpf Posted October 6, 2007 Author Share Posted October 6, 2007 Why is the "some random number" thing there? Quote Link to comment https://forums.phpfreaks.com/topic/71754-solved-help-with-finding-difference-in-hours-between-two-times/#findComment-363087 Share on other sites More sharing options...
heckenschutze Posted October 6, 2007 Share Posted October 6, 2007 Just for example, so the times were different Can chuck it in a function for a one liner, function diffHours($a, $b) { return (abs($a - $b) / pow(60, 2)); } abs() because we only care about the difference in scalar terms. Quote Link to comment https://forums.phpfreaks.com/topic/71754-solved-help-with-finding-difference-in-hours-between-two-times/#findComment-363090 Share on other sites More sharing options...
kernelgpf Posted October 6, 2007 Author Share Posted October 6, 2007 Oh.. well, how do I feed the "$row[datetillcompletion]" into it? Sorry- I'm still learning PHP time code.. it confuses me. =[ Quote Link to comment https://forums.phpfreaks.com/topic/71754-solved-help-with-finding-difference-in-hours-between-two-times/#findComment-363092 Share on other sites More sharing options...
heckenschutze Posted October 6, 2007 Share Posted October 6, 2007 $hours = diffHours($row['datetillcompletion'], time()); Returns the amount of hours between the 'datetillcompletion' and the current time. Note: It really helps if you understand unix timestamps, no need to convert it to a string, then back to a timestamp. Quote Link to comment https://forums.phpfreaks.com/topic/71754-solved-help-with-finding-difference-in-hours-between-two-times/#findComment-363097 Share on other sites More sharing options...
kernelgpf Posted October 6, 2007 Author Share Posted October 6, 2007 I don't understand unix timestamps.. I'll have to look at that when I get a chance. I tried your code, and the result I got was "331009.52944444"... I can round it, but the timestamp in the DB for "$row[datetillcompletion]" is "2007-10-06 10:00:00"... that isn't right. Any other suggestions? Quote Link to comment https://forums.phpfreaks.com/topic/71754-solved-help-with-finding-difference-in-hours-between-two-times/#findComment-363103 Share on other sites More sharing options...
heckenschutze Posted October 6, 2007 Share Posted October 6, 2007 diffHours() will tell you the difference between 2 unix timestamps, in hours. A unix timestamp is how many seconds have past since the UNIX EPOCH (1970), once you realize that it's pretty easy. You should be storing the timestamp in the database, not a string. Quote Link to comment https://forums.phpfreaks.com/topic/71754-solved-help-with-finding-difference-in-hours-between-two-times/#findComment-363107 Share on other sites More sharing options...
kernelgpf Posted October 6, 2007 Author Share Posted October 6, 2007 ...now I'm confused. =/ What exactly do I need to do to fix it, then? Thanks alot for your help. =] Quote Link to comment https://forums.phpfreaks.com/topic/71754-solved-help-with-finding-difference-in-hours-between-two-times/#findComment-363109 Share on other sites More sharing options...
heckenschutze Posted October 6, 2007 Share Posted October 6, 2007 Nothing really to fix it, $hours = diffHours(strtotime($row3['datetillcompletion']), time()); Does that work? you just need to pass 2 timestamps to diffHours. strtotime() converts a string to a timestamp. You should consider changing your database so dates/times are stored as unix timestamps, you can get the current time with time(). Quote Link to comment https://forums.phpfreaks.com/topic/71754-solved-help-with-finding-difference-in-hours-between-two-times/#findComment-363111 Share on other sites More sharing options...
kernelgpf Posted October 6, 2007 Author Share Posted October 6, 2007 I did that, and it's what gave me "33019.54444" hours, or something. What column type should I change the column to? It's "timestamp" right now, and I don't see a UNIX option. Quote Link to comment https://forums.phpfreaks.com/topic/71754-solved-help-with-finding-difference-in-hours-between-two-times/#findComment-363113 Share on other sites More sharing options...
heckenschutze Posted October 6, 2007 Share Posted October 6, 2007 integer Quote Link to comment https://forums.phpfreaks.com/topic/71754-solved-help-with-finding-difference-in-hours-between-two-times/#findComment-363123 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.