Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // patterns
- $numsPattern = '/[^a-zA-Z0-9]+?([0-9]+)[^a-zA-Z0-9]+?/';
- $datePattern = '/([0-9]{4})-([0-9]{2})-([0-9]{2})/';
- // extracting information
- preg_match_all($numsPattern, $_GET['numbersString'], $nums);
- preg_match_all($datePattern, $_GET['dateString'], $dates, PREG_SET_ORDER);
- // getting sum backwards
- $backwardsNum = numBackwards(array_sum($nums[1]));
- // Printing results
- if(!empty($dates)) {
- echo '<ul>';
- foreach($dates as $d) {
- $newDate = new DateTime($d[0]);
- // var_dump($newDate); // date befor modification
- addDays($newDate, $backwardsNum);
- // var_dump($newDate); // modified date with $backwardsNum
- echo '<li>' . $newDate->format('Y-m-d') . '</li>';
- }
- echo '</ul>';
- } else {
- echo '<p>No dates</p>';
- }
- function numBackwards($num) {
- $numStr = (string) $num;
- preg_match_all("/[0-9]/", $numStr, $numArr);
- return (int) implode(array_reverse($numArr[0]));
- }
- function addDays(DateTime $date, $days) {
- $date->add(new DateInterval('P' . $days . 'D'));
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement