Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class TimeTravel {
- public $start;
- public $end;
- public function __construct($date1, $date2 = "") {
- $this->start = new DateTime($date1);
- $this->end = new DateTime($date2);
- }
- public function getTravelInfo() {
- $interval = $this->start->diff($this->end);
- 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>";
- }
- public function findDate($interval) {
- if($interval>=0){
- $newInterval = DateInterval::createFromDateString($interval);
- $this->end = $this->start->sub($newInterval);
- } else {
- $interval *= -1;
- $newInterval = DateInterval::createFromDateString($interval);
- $this->end = $this->start->add($newInterval);
- }
- return $this->end;
- }
- public function backToFutureStepByStep($step) {
- $today = new DateTime();
- $array = [];
- $array[0] = $this->start;
- $newStep = new DateInterval($step);
- for($i=1; $array[$i-1]<$today; $i++) {
- $temp = clone $array[$i-1];
- $array[$i] = $temp->add($newStep);
- }
- return $array;
- }
- };
- $travel = new TimeTravel("1985/12/31", "today");
- echo $travel->getTravelInfo();
- //$datePast = $travel->findDate('1000000000 seconds');
- //echo $datePast->format('d/m/Y');
- $travel->findDate('1000000000 seconds');
- echo $travel->end->format('d/m/Y');
- $timeArray = $travel->backToFutureStepByStep('P1M1W1D');
- foreach($timeArray as $element) {
- echo "<br>" . $element->format('M d Y A H:i');
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement