Fork me on GitHub

Picking up where strtotime() left off

Picking up where strtotime() left off

I was working on a project where dates were being calculated (sort of) based on user input. For example the user was able to type in “Next Saturday” or the “Second Sunday of the Month”. This wouldn’t really be sortable, as strtotime doesn’t exactly allow all of these inputs, and so the saga of extending the functionality to accomodate almost any user input (well what i found common use for this client anyway) began.

This function will spit out timestamps of the following dates for example:

Sat 5 – Sun 27 Jun 10
Sun 6 Jun 2010
Tue 29 Jun – Sat 3 Jul + Tue 6 Jul – Sat 10 Jul 2010
Thu 29 Jul – Sat 31 Jul + Wed 4 Aug – Sat 7 Aug 2010
Every Fri
1st Sat of Every Month
3rd Wed of Every Month

This was developed for a particular use case, so please, if you would like some other terms thrown in let me know and I might go ahead and build them in for future use.

function calculateStartDates($data) {
	foreach ($data as &$event) {
		if (!empty($event['Event']['time_period'])) {
			$event['Event']['start_timestamp'] = $event['Event']['time_period'];
			preg_match('/(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)/i', $event['Event']['start_timestamp'], $months);
			preg_match('/(.*)everys*(w+)/i', $event['Event']['start_timestamp'], $every);
			$datePlurals = array('first', 'second', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eigth', 'nineth');
			$fullDays = array('sat' => 'Saturday', 'sun' => 'Sunday', 'mon' => 'Monday', 'tue' => 'Tuesday', 'wed' => 'Wednesday', 'thu' => 'Thursday', 'fri' => 'Friday');

			if (!empty($every[2])) {
				if (stristr($every[2], 'month')) {
					preg_match('/(mon|tue|wed|thur|fri|sat|sun)/i', $event['Event']['start_timestamp'], $day);
					preg_match_all('/(d)(nd|st|th|rd)/i', $every[1], $times);
					if (!empty($times[1][0])) {
						$event['Event']['start_timestamp'] = date('d-m-Y', strtotime($datePlurals[$times[1][0]-1].' '.$fullDays[strtolower($day[1])]));
					}
				} else {
					$event['Event']['start_timestamp'] = date('d-m-Y', strtotime('next '.$every[2]));
				}
			} else {
				$dashMatch = split('[-+]', $event['Event']['start_timestamp']);

				foreach ($dashMatch as $timeFrame) {
					preg_match('/(jan|feb|mar|apr|may|jun|jul|aug|sep|oct|nov|dec)/i', $timeFrame, $thisMonth);

					if (!empty($months[1]) && empty($thisMonth[1])) {
						if (!stristr($timeFrame, $months[1])) {
							$timeFrame = $timeFrame.' '.$months[1];
						}
					}
					$tempTime[] = strtotime($timeFrame);
				}

				sort($tempTime);
				unset($setTime, $tempTime);
				if (!empty($tempTime)) {
					foreach ($tempTime as $time) {
						if ($time > mktime()) {
							$setTime = date('d-m-Y', $time);
							break;
						}
					}
				}

				if (!empty($timeFrame) && empty($setTime)) {
					$event['Event']['start_timestamp'] = $timeFrame;
				} elseif (!empty($setTime)) {
					$event['Event']['start_timestamp'] = $setTime;
				}

			}
			$event['Event']['start_timestamp'] = strtotime($event['Event']['start_timestamp']);
		}
	}
	return $data;
}

Posted by voidet

Categorised under PHP
Bookmark the permalink or leave a trackback.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

or