Posted By

Scooter on 02/07/09


Tagged

fun Star Trek


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

luman


Difference between two stardates


 / Published in: PHP
 

URL: http://reusablecode.blogspot.com/2009/02/difference-between-two-stardates.html

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

  1. <?php
  2. /*
  3.   Copyright (c) 2009, reusablecode.blogspot.com; some rights reserved.
  4.  
  5.   This work is licensed under the Creative Commons Attribution License. To view
  6.   a copy of this license, visit http://creativecommons.org/licenses/by/3.0/ or
  7.   send a letter to Creative Commons, 559 Nathan Abbott Way, Stanford, California
  8.   94305, USA.
  9.   */
  10.  
  11. // Calculate the difference between two stardates.
  12. function stardatediff($firstStardate, $secondStardate)
  13. {
  14. $years1 = substr($firstStardate, 0, 2);
  15. $years2 = substr($secondStardate, 0, 2);
  16.  
  17. $days1 = substr($firstStardate, 2, 3);
  18. $days2 = substr($secondStardate, 2, 3);
  19.  
  20. $hours1 = substr($firstStardate, 6, 1);
  21. $hours2 = substr($secondStardate, 6, 1);
  22.  
  23. if ($years1 > $years2)
  24. {
  25. $years = $years1 - $years2;
  26. }
  27. else
  28. {
  29. $years = $years2 - $years1;
  30. }
  31.  
  32. if ($days1 > $days2)
  33. {
  34. $days = ($days1 * 366 / 1000) - ($days2 * 366 / 1000);
  35. }
  36. else
  37. {
  38. $days = ($days2 * 366 / 1000) - ($days1 * 366 / 1000);
  39. }
  40.  
  41. if ($hours1 > $hours2)
  42. {
  43. $hours = ($hours1 * 144 / 60) - ($hours2 * 144 / 60);
  44. }
  45. else
  46. {
  47. $hours = ($hours2 * 144 / 60) - ($hours1 * 144 / 60);
  48. }
  49.  
  50. $days = round($days);
  51. $hours = round($hours, 1);
  52.  
  53. return "There are $years years, $days days, and $hours hours between Stardate $firstStardate and Stardate $secondStardate.";
  54. }
  55. ?>

Report this snippet  

You need to login to post a comment.