Guest User

Untitled

a guest
May 7th, 2018
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Custom exception class required by Countdown
  5. *
  6. * @package countdown
  7. * @author Caius Durling <dev@caius.name>
  8. */
  9. class InvalidTimeGiven extends Exception {}
  10.  
  11. /**
  12. * Countdown class
  13. *
  14. * Counts down to a given date returning a
  15. * variety of formats and options
  16. *
  17. * @version 0.5
  18. * @package countdown
  19. * @author Caius Durling <dev@caius.name>
  20. */
  21. class Countdown
  22. {
  23. /**
  24. * The ending time stored as seconds since the epoch
  25. *
  26. * @var string
  27. */
  28. private $time;
  29.  
  30. /**
  31. * Kicks it off
  32. *
  33. * @param string $time
  34. * @author Caius Durling <dev@caius.name>
  35. */
  36. function __construct( $time )
  37. {
  38. $this->set_time( $time );
  39. }
  40.  
  41. /**
  42. * Kicks it off, lets you use Countdown in one line.
  43. * eg: <code>Countdown::create("9th November 2008 11:00")->out('weeks');</code>
  44. *
  45. * @param string $time
  46. * @return Countdown
  47. * @author Caius Durling <dev@caius.name>
  48. */
  49. public static function create( $time )
  50. {
  51. return new Countdown($time);
  52. }
  53.  
  54. /**
  55. * Outputs the time remaining, including the time
  56. * counting down to if requested.
  57. *
  58. * @param string $type What format to return the countdown time in.
  59. * @param bool $include_time Whether to include the time counting down to or not
  60. * @param string $format What format to return the counting down time in
  61. * @return string
  62. * @author Caius Durling <dev@caius.name>
  63. */
  64. public function out( $type="seconds", $include_time = true, $format = "%c" )
  65. {
  66. switch ( $type ) {
  67. # I'm sure theres a better way to do this dividing
  68. case 'weeks':
  69. $num = (((($this->time_diff() / 60) / 60) / 24) / 7);
  70. break;
  71.  
  72. case 'days':
  73. $num = ((($this->time_diff() / 60) / 60) / 24);
  74. break;
  75.  
  76. case 'hours':
  77. $num = (($this->time_diff() / 60) / 60);
  78. break;
  79.  
  80. case 'minutes':
  81. $num = ($this->time_diff() / 60);
  82. break;
  83.  
  84. case 'seconds':
  85. $num = $this->time_diff();
  86. break;
  87.  
  88. default:
  89. $num = $this->time_diff();
  90. break;
  91. }
  92. $num = round( $num );
  93. $out = "{$num} {$type}";
  94. if ( $include_time ) {
  95. $out .= " to " . strftime( $format, $this->time );
  96. }
  97.  
  98. return $out;
  99. }
  100.  
  101. /**
  102. * Calculates time remaining in seconds
  103. *
  104. * @param string $time Time to count from.
  105. * @return integer difference between $time and $this->time
  106. * @author Caius Durling <dev@caius.name>
  107. */
  108. private function time_diff( $time = null )
  109. {
  110. # PHP is stupid, I should be able to do:
  111. # function time_diff( $time = time() ) {}
  112. # or $time ||= time();
  113. if ( $time === null ) {
  114. $time = time();
  115. }
  116.  
  117. return ($this->time - $time);
  118. }
  119.  
  120. /**
  121. * Parses the passed string into seconds since the epoch
  122. *
  123. * @param string $time Time you want to parse
  124. * @return void
  125. * @author Caius Durling <dev@caius.name>
  126. */
  127. private function set_time( $time )
  128. {
  129. $this->time = strtotime( $time );
  130.  
  131. if ( $this->time === false ) {
  132. throw new InvalidTimeGiven;
  133. }
  134. }
  135. }
  136.  
  137. echo Countdown::create("9th November 2008 11:00")->out('weeks');
  138. # => "5 weeks to Sun Nov 9 11:00:00 2008"
  139.  
  140. ?>
Add Comment
Please, Sign In to add comment