Advertisement
Guest User

Untitled

a guest
Sep 25th, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.50 KB | None | 0 0
  1. <?php
  2.  
  3. class TimeTravel {
  4. public $start;
  5. public $end;
  6.  
  7. public function __construct($date1, $date2 = "") {
  8. $this->start = new DateTime($date1);
  9. $this->end = new DateTime($date2);
  10. }
  11.  
  12. public function getTravelInfo() {
  13. $interval = $this->start->diff($this->end);
  14. return "Il y a " . $interval->y . " années, " . $interval->m . " mois, " . $interval->d . " jours, " . $interval->h . " heures, " . $interval->i . " minutes, et " . $interval->s . " secondes entre les deux dates.<br>";
  15. }
  16.  
  17. public function findDate($interval) {
  18. if($interval>=0){
  19. $newInterval = DateInterval::createFromDateString($interval);
  20. $this->end = $this->start->sub($newInterval);
  21. } else {
  22. $interval *= -1;
  23. $newInterval = DateInterval::createFromDateString($interval);
  24. $this->end = $this->start->add($newInterval);
  25. }
  26. return $this->end;
  27. }
  28.  
  29. public function backToFutureStepByStep($step) {
  30. $today = new DateTime();
  31. $array = [];
  32. $array[0] = $this->start;
  33. $newStep = new DateInterval($step);
  34. for($i=1; $array[$i-1]<$today; $i++) {
  35. $temp = clone $array[$i-1];
  36. $array[$i] = $temp->add($newStep);
  37. }
  38.  
  39. return $array;
  40. }
  41. };
  42.  
  43. $travel = new TimeTravel("1985/12/31", "today");
  44. echo $travel->getTravelInfo();
  45.  
  46. //$datePast = $travel->findDate('1000000000 seconds');
  47. //echo $datePast->format('d/m/Y');
  48. $travel->findDate('1000000000 seconds');
  49. echo $travel->end->format('d/m/Y');
  50.  
  51. $timeArray = $travel->backToFutureStepByStep('P1M1W1D');
  52. foreach($timeArray as $element) {
  53. echo "<br>" . $element->format('M d Y A H:i');
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement