Have you ever got difficulty when you want to calculate the difference or the time duration between two dates by using PHP? For example, the first date is ’2009-12-01 11:12:13′, and the second date is ’2009-12-04 14:15:16′, so the difference or the time duration between this two dates will give a result as: ’3 Day(s), 3 Hour(s), 3 Minute(s), 3 Second(s).’. We will make the function in order to make you easy to implement this calculation in your PHP code. Please note that your date format must be in this order: ‘yyyy-MM-dd hh:mm:ss’ as possible, otherwise, this function will not work properly, I guess.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php /** * @author Masino Sinaga, http://www.openscriptsolution.com * @copyright December 14, 2009 */ // Display the result... echo Duration('2009-12-01 11:12:13', '2009-12-04 14:15:16'); function Duration($parambegindate, $paramenddate) { $begindate = strtotime($parambegindate); $enddate = strtotime($paramenddate); $diff = intval($enddate) - intval($begindate); $diffday = intval(floor($diff/86400)); $modday = ($diff%86400); $diffhour = intval(floor($modday/3600)); $diffminute = intval(floor(($modday%3600)/60)); $diffsecond = ($modday%60); return round($diffday)." Day(s), ".round($diffhour)." Hour(s), ".round($diffminute,0)." Minute(s), ".round($diffsecond,0)." Second(s)."; } ?> |

Recent Comments