# Display Time That Passed Since a Date

```Calculate the time that has passed since a specified Unix formatted date and time, and show it in a human readable format, all in a PHP function.
```
``````function TimeSince(\$original) // \$original should be the original date and time in Unix format
{
// Common time periods as an array of arrays
\$periods = array(
array(60 * 60 * 24 * 365 , 'year'),
array(60 * 60 * 24 * 30 , 'month'),
array(60 * 60 * 24 * 7, 'week'),
array(60 * 60 * 24 , 'day'),
array(60 * 60 , 'hour'),
array(60 , 'minute'),
);

\$today = time();
\$since = \$today - \$original; // Find the difference of time between now and the past

// Loop around the periods, starting with the biggest
for (\$i = 0, \$j = count(\$periods); \$i < \$j; \$i++)
{
\$seconds = \$periods[\$i][0];
\$name = \$periods[\$i][1];

// Find the biggest whole period
if ((\$count = floor(\$since / \$seconds)) != 0)
{
break;
}
}

\$output = (\$count == 1) ? '1 '.\$name : "\$count {\$name}s";

if (\$i + 1 < \$j)
{
// Retrieving the second relevant period
\$seconds2 = \$periods[\$i + 1][0];
\$name2 = \$periods[\$i + 1][1];

// Only show it if it's greater than 0
if ((\$count2 = floor((\$since - (\$seconds * \$count)) / \$seconds2)) != 0)
{
\$output .= (\$count2 == 1) ? ', 1 '.\$name2 : ", \$count2 {\$name2}s";
}
}
return \$output;
}

echo TimeSince(1249091056);``````

The provided code is a PHP function named `TimeSince` designed to calculate the time elapsed since a given date and time, specified in Unix format (Unix timestamp). It returns a human-readable string representing this time difference. Here’s an explanation:

1. Function Definition: `function TimeSince(\$original)` takes one argument, `\$original`, which is the date and time in Unix timestamp format.
2. Time Periods Array: The `\$periods` array defines various time periods in seconds (years, months, weeks, days, hours, minutes) along with their textual representation.
3. Current Time Calculation: `\$today = time();` gets the current time in Unix timestamp format. `\$since = \$today - \$original;` calculates the difference in seconds between the current time and the provided timestamp.
4. Calculating the Largest Time Period: The `for` loop iterates through the `\$periods` array, starting from the largest time unit (year). It calculates how many of each unit fits into the `\$since` time difference. Once it finds a time unit that fits (`floor(\$since / \$seconds) != 0`), it breaks out of the loop.
5. Formatting the Output: `\$output` is formatted to show the elapsed time in the largest fitting time unit found. If the count is 1, it uses the singular form of the unit; otherwise, it uses the plural.
6. Adding a Second Time Period: The code then checks if there’s a smaller time unit that can also be used to describe the remaining time. If so, it appends this information to the `\$output`.
7. Returning the Result: The function returns the formatted string `\$output`, which describes the time elapsed since the original timestamp in a human-readable format.
8. Example Usage: `echo TimeSince(1249091056);` This line would output the time elapsed since the Unix timestamp `1249091056`.
##### Nathan
Nathan Pakovskie is an esteemed senior developer and educator in the tech community, best known for his contributions to Geekpedia.com. With a passion for coding and a knack for simplifying complex tech concepts, Nathan has authored several popular tutorials on C# programming, ranging from basic operations to advanced coding techniques. His articles, often characterized by clarity and precision, serve as invaluable resources for both novice and experienced programmers. Beyond his technical expertise, Nathan is an advocate for continuous learning and enjoys exploring emerging technologies in AI and software development. When he’s not coding or writing, Nathan engages in mentoring upcoming developers, emphasizing the importance of both technical skills and creative problem-solving in the ever-evolving world of technology. Specialties: C# Programming, Technical Writing, Software Development, AI Technologies, Educational Outreach