My Ride
Started: Wednesday 19th June 2013 4:52pm
Distance: 25.47km
Duration: 00:53:28
Rest Time: 00:03:38
Climb: 219m
Max Speed: 57.96kmph
Average Speed: 28.58kmphInstagrams
-
Recent Posts
Recent Comments
Archives
- February 2013
- December 2012
- September 2012
- July 2012
- January 2012
- September 2011
- August 2011
- February 2011
- January 2011
- November 2010
- October 2010
- August 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
Categories
Meta
Picking up where strtotime() left off
May 6, 2010,
853 views
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;
}
Categorised under PHP





