Advertisement
Guest User

PHP | Future Dates | Exam 22 Dec 2014

a guest
Apr 13th, 2015
363
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.18 KB | None | 0 0
  1. <?php  
  2.     // patterns
  3.     $numsPattern = '/[^a-zA-Z0-9]+?([0-9]+)[^a-zA-Z0-9]+?/';
  4.     $datePattern = '/([0-9]{4})-([0-9]{2})-([0-9]{2})/';
  5.  
  6.     // extracting information
  7.     preg_match_all($numsPattern, $_GET['numbersString'], $nums);
  8.     preg_match_all($datePattern, $_GET['dateString'], $dates, PREG_SET_ORDER);
  9.  
  10.     // getting sum backwards
  11.     $backwardsNum = numBackwards(array_sum($nums[1]));
  12.  
  13.     // Printing results
  14.     if(!empty($dates)) {
  15.         echo '<ul>';
  16.         foreach($dates as $d) {
  17.             $newDate = new DateTime($d[0]);
  18.             // var_dump($newDate); // date befor modification
  19.             addDays($newDate, $backwardsNum);
  20.             // var_dump($newDate); // modified date with $backwardsNum
  21.             echo '<li>' . $newDate->format('Y-m-d') . '</li>';
  22.         }
  23.         echo '</ul>';
  24.     } else {
  25.         echo '<p>No dates</p>';
  26.     }
  27.  
  28.     function numBackwards($num) {
  29.         $numStr = (string) $num;
  30.         preg_match_all("/[0-9]/", $numStr, $numArr);
  31.         return (int) implode(array_reverse($numArr[0]));
  32.     }
  33.  
  34.     function addDays(DateTime $date, $days) {
  35.         $date->add(new DateInterval('P' . $days . 'D'));
  36.     }
  37. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement