note 108087 added to function.date-diff
| From: | holdoffhunger at gmail dot com | Date: | Tue, 27 Mar 2012 21:37:56 +0000 |
| Subject: | note 108087 added to function.date-diff | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186877@lists.php.net to get a copy of this message | ||
I wanted to have a formatted time difference function that worked with two time() integer results
and formatted the results, such as "X Years, Y Months, Z Weeks, etc.." The following is
that function, and it's designed to be modifiable, so you could easily cut out the standard
Years / Months / Weeks / Days / Hours / Minutes / Seconds Format, and go to whatever format you
prefer in whatever order you prefer.
<?php
// Author: holdoffhunger@gmail.com
// Define Input
// --------------------------------------
// Example Time Difference
$start_time_for_conversion = 0;
$end_time_for_conversion = 123456;
$difference_of_times = $end_time_for_conversion - $start_time_for_conversion;
$time_difference_string = "";
for($i_make_time = 6; $i_make_time > 0; $i_make_time--)
{
switch($i_make_time)
{
// Handle Minutes
// ........................
case '1';
$unit_title = "Minute(s)";
$unit_size = 60;
break;
// Handle Hours
// ........................
case '2';
$unit_title = "Hour(s)";
$unit_size = 3600;
break;
// Handle Days
// ........................
case '3';
$unit_title = "Day(s)";
$unit_size = 86400;
break;
// Handle Weeks
// ........................
case '4';
$unit_title = "Week(s)";
$unit_size = 604800;
break;
// Handle Months (31 Days)
// ........................
case '5';
$unit_title = "Month(s)";
$unit_size = 2678400;
break;
// Handle Years (365 Days)
// ........................
case '6';
$unit_title = "Year(s)";
$unit_size = 31536000;
break;
}
if($difference_of_times > ($unit_size - 1))
{
$modulus_for_time_difference = $difference_of_times % $unit_size;
$seconds_for_current_unit = $difference_of_times - $modulus_for_time_difference;
$units_calculated = $seconds_for_current_unit / $unit_size;
$difference_of_times = $modulus_for_time_difference;
$time_difference_string .= "$units_calculated $unit_title, ";
}
}
// Handle Seconds
// ........................
$time_difference_string .= "$difference_of_times Second(s)";
// Example Result: "1 Day(s), 10 Hour(s), 17 Minute(s), 36 Second(s)"
?>
One other example: 123456789 = "3 Year(s), 10 Month(s), 3 Week(s), 2 Day(s), 21 Hour(s), 33
Minute(s), 9 Second(s)"
----
Server IP: 69.147.83.197
Probable Submitter: 68.9.137.178
----
Manual Page -- http://www.php.net/manual/en/function.date-diff.php
Edit -- https://master.php.net/note/edit/108087
Del: integrated -- https://master.php.net/note/delete/108087/integrated
Del: useless -- https://master.php.net/note/delete/108087/useless
Del: bad code -- https://master.php.net/note/delete/108087/bad+code
Del: spam -- https://master.php.net/note/delete/108087/spam
Del: non-english -- https://master.php.net/note/delete/108087/non-english
Del: in docs -- https://master.php.net/note/delete/108087/in+docs
Del: other reasons-- https://master.php.net/note/delete/108087
Reject -- https://master.php.net/note/reject/108087
Search -- https://master.php.net/manage/user-notes.php