[PHP] Date Subtraction
I have two periods in time from a Cisco router that I would like to find the difference in seconds. I am not sure the best way to do this since it is not a date, but rather an amount of time since last reset. Here is the numbers 181 days, 7:11:06.66 //stands for 181 days, 7 hours, 11 minutes, 6.66 seconds. The next is 181 days, 7:16:6.75 //stands for 181 days, 7 hours, 16 minutes, 7.75 seconds. I would probably shave off the .66 and .75 seconds while it was still a string. It may be faster to round when it's in seconds, I really don't know. Then what do I do? I was thinking of using strtotime(); but because it's not really a date, I am at a loss for what to do. Any help with this would be great. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Date Subtraction
I don't think you can use strtotime in that case. However, assuming that your data is properly formatted every time, you can use a simple function like this (I'm doing it from memory, so it might not actually work): ?php function conv_time ($s) { preg_match (/^([0-9]*) days, ([0-9]*)\:([0-9]*)\:([0-9]*)(\.[0-9]*)$/, $s, $res); return $res[1] * 86400 + $res[2] * 3600 + $res[3] * 60 + $res[4] + $res[5]; } echo number_format (conv_time (181 days, 7:16:6.75) - conv_time (181 days, 7:11:06.66), 2); ? Hope this helps. Cheers, Marco -- php|architect - The Magazine for PHP Professionals The monthly magazine dedicated to the world of PHP programming Check us out on the web at http://www.phparch.com! ---BeginMessage--- I have two periods in time from a Cisco router that I would like to find the difference in seconds. I am not sure the best way to do this since it is not a date, but rather an amount of time since last reset. Here is the numbers 181 days, 7:11:06.66 //stands for 181 days, 7 hours, 11 minutes, 6.66 seconds. The next is 181 days, 7:16:6.75 //stands for 181 days, 7 hours, 16 minutes, 7.75 seconds. I would probably shave off the .66 and .75 seconds while it was still a string. It may be faster to round when it's in seconds, I really don't know. Then what do I do? I was thinking of using strtotime(); but because it's not really a date, I am at a loss for what to do. Any help with this would be great. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php ---End Message--- -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Date Subtraction
okay, this is just me thinking out loud... none of this is tested... ? function something($time) { $time = substr($time, 0, -3); list($days,$theRest) = explode(' days, ', $time); list($h,$m,$s) = explode(':', $theRest); $days = $days * 86400; $h = $h * 360; $m = $m * 60; $seconds = $days+$h+$m+$s; return $seconds; } $t1 = something('181 days, 7:16:6.75'); $t2 = something('181 days, 7:11:06.66'); echo $t1 - $t2; ? basically, - take the string - get rid of the last 3 chars (eg .75) - split on ' days, ' - split the 2nd half on ':' - multiply each bit (days, hrs, mins) by the appropriate no of seconds - add it all together to get a number in seconds Cheers, Justin on 24/12/02 8:29 AM, Christopher J. Crane ([EMAIL PROTECTED]) wrote: I have two periods in time from a Cisco router that I would like to find the difference in seconds. I am not sure the best way to do this since it is not a date, but rather an amount of time since last reset. Here is the numbers 181 days, 7:11:06.66 //stands for 181 days, 7 hours, 11 minutes, 6.66 seconds. The next is 181 days, 7:16:6.75 //stands for 181 days, 7 hours, 16 minutes, 7.75 seconds. I would probably shave off the .66 and .75 seconds while it was still a string. It may be faster to round when it's in seconds, I really don't know. Then what do I do? I was thinking of using strtotime(); but because it's not really a date, I am at a loss for what to do. Any help with this would be great. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Date subtraction
Do it in MySQL = Fast Don't fiddle in PHP = waste of your time. Look into the following: select UNIX_TIMESTAMP(date1)-UNIX_TIMESTAMP(date1) as diff_secs, TO_DAYS(date1)-TO_DAYS(date1) as diff_days, PERIOD_DIFF(date1,date2)as diff_months, YEAR(date1) - YEAR(date2) as diff_years from MYTABLE where id=$id I'm not sure what select date1-date2 as date_diff returns? beware of the 2037 limitation using UNIX_TIMESTAMP() regards Jeff -Original Message- From: BlackLord [mailto:[EMAIL PROTECTED]] Sent: Thursday, March 22, 2001 5:41 PM To: [EMAIL PROTECTED] Subject: [PHP] Date subtraction Hi! I want to subtract to date times from each other. Like : '2000 12 01 12:12:12' - '2000 11 10 11:39:59' Is there any function to do this subtraction or can i do it with MySQL's SELECT query ? Thanks -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Date subtraction
thanks for your help. ""Jeff Armstrong"" [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... Do it in MySQL = Fast Don't fiddle in PHP = waste of your time. Look into the following: select UNIX_TIMESTAMP(date1)-UNIX_TIMESTAMP(date1) as diff_secs, TO_DAYS(date1)-TO_DAYS(date1) as diff_days, PERIOD_DIFF(date1,date2)as diff_months, YEAR(date1) - YEAR(date2) as diff_years from MYTABLE where id=$id I'm not sure what select date1-date2 as date_diff returns? beware of the 2037 limitation using UNIX_TIMESTAMP() regards Jeff -Original Message- From: BlackLord [mailto:[EMAIL PROTECTED]] Sent: Thursday, March 22, 2001 5:41 PM To: [EMAIL PROTECTED] Subject: [PHP] Date subtraction Hi! I want to subtract to date times from each other. Like : '2000 12 01 12:12:12' - '2000 11 10 11:39:59' Is there any function to do this subtraction or can i do it with MySQL's SELECT query ? Thanks -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
[PHP] Date subtraction
Hi! I want to subtract to date times from each other. Like : '2000 12 01 12:12:12' - '2000 11 10 11:39:59' Is there any function to do this subtraction or can i do it with MySQL's SELECT query ? Thanks -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]
Re: [PHP] Date subtraction
PERIOD_DIFF(P1,P2) or convert them both tu unix_time(p1) - unix_time(p2) then you can have seconds and format them as you want almir ""BlackLord"" [EMAIL PROTECTED] schrieb im Newsbeitrag 99dcs5$cbh$[EMAIL PROTECTED]">news:99dcs5$cbh$[EMAIL PROTECTED]... Hi! I want to subtract to date times from each other. Like : '2000 12 01 12:12:12' - '2000 11 10 11:39:59' Is there any function to do this subtraction or can i do it with MySQL's SELECT query ? Thanks -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] To contact the list administrators, e-mail: [EMAIL PROTECTED]