Guest User

Untitled

a guest
Jun 25th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. <?php
  2.  
  3. class TimeTravel {
  4.  
  5. private $start;
  6. private $end;
  7.  
  8. public function __construct(DateTime $startDate, DateTime $endDate) {
  9.  
  10. $this->startDate = $startDate;
  11. $this->endDate = $endDate;
  12. }
  13.  
  14.  
  15. public function getTravelInfo():string {
  16. $diff = $this->startDate->diff($this->endDate);
  17. return $diff->format('Il y a %y années, %m mois, %d jours, %h heures, %i minutes et %s secondes entre les deux dates');
  18. }
  19.  
  20. public function findDate(int $interval):DateTime {
  21.  
  22. $dateInterval = new DateInterval('PT' . $interval .'S' );
  23. return $this->startDate->sub($dateInterval);
  24.  
  25. }
  26.  
  27. public function backToFutureStepByStep($step):array {
  28.  
  29. $result = [];
  30. $dateRange = new DatePeriod($this->startDate, $step, $this->endDate);
  31.  
  32. foreach ($dateRange as $date) {
  33.  
  34. $result[] = $date->format('M-d-y A-i:H:s');
  35.  
  36. }
  37. return $result;
  38. }
  39.  
  40. }
  41.  
  42.  
  43. $timeTrip = new TimeTravel(new DateTime('1985-12-31'), new DateTime());
  44. echo $timeTrip->getTravelInfo();
  45.  
  46. $retour = $timeTrip->findDate(1000000000);
  47. echo "<br>". $retour->format("Y-m-d") . "<br>";
  48.  
  49. $convecteur = $timeTrip->backToFutureStepByStep(new DateInterval('P1M1W1D'));
  50. var_dump($convecteur);
Add Comment
Please, Sign In to add comment