## Posted By

Scooter on 02/07/09

## Who likes this?

1 person have marked this snippet as a favorite

# Difference between two stardates

/ Published in: PHP

Calculates the difference between two Stardates and returns the result as a human-readable string.

`<?php    /*    Copyright (c) 2009, reusablecode.blogspot.com; some rights reserved.        This work is licensed under the Creative Commons Attribution License. To view    a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or    send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California    94305, USA.    */ 	// Calculate the difference between two stardates.	function stardatediff(\$firstStardate, \$secondStardate)	{        \$years1 = substr(\$firstStardate, 0, 2);        \$years2 = substr(\$secondStardate, 0, 2);         \$days1 = substr(\$firstStardate, 2, 3);        \$days2 = substr(\$secondStardate, 2, 3);         \$hours1 = substr(\$firstStardate, 6, 1);        \$hours2 = substr(\$secondStardate, 6, 1);         if (\$years1 > \$years2)        {            \$years = \$years1 - \$years2;        }        else        {            \$years = \$years2 - \$years1;        }         if (\$days1 > \$days2)        {            \$days = (\$days1 * 366 / 1000) - (\$days2 * 366 / 1000);        }        else        {            \$days = (\$days2 * 366 / 1000) - (\$days1 * 366 / 1000);        }         if (\$hours1 > \$hours2)        {            \$hours = (\$hours1 * 144 / 60) - (\$hours2 * 144 / 60);        }        else        {            \$hours = (\$hours2 * 144 / 60) - (\$hours1 * 144 / 60);        }         \$days = round(\$days);        \$hours = round(\$hours, 1);         return "There are \$years years, \$days days, and \$hours hours between Stardate \$firstStardate and Stardate \$secondStardate.";	}?>`