+ /**
+ * Check if the value returned by a date picker has a date section (ie: includes
+ * a '-' character) if it includes a time section (ie: includes a ':').
+ *
+ * @param string $value
+ * A date/time string input from a datepicker value.
+ *
+ * @return bool
+ * TRUE if valid, FALSE if there is a time without a date.
+ */
+ public static function datePickerValueWithTimeHasDate($value) {
+ // If there's no : (time) or a : and a - (date) then return true
+ return (
+ strpos($value, ':') === FALSE
+ || strpos($value, ':') !== FALSE && strpos($value, '-') !== FALSE
+ );
+ }
+
+ /**
+ * Validate start and end dates entered on a form to make sure they are
+ * logical. Expects the form keys to be start_date and end_date.
+ *
+ * @param string $startFormKey
+ * The form element key of the 'start date'
+ * @param string $startValue
+ * The value of the 'start date'
+ * @param string $endFormKey
+ * The form element key of the 'end date'
+ * @param string $endValue
+ * The value of the 'end date'
+ *
+ * @return array|bool
+ * TRUE if valid, an array of the erroneous form key, and error message to
+ * use otherwise.
+ */
+ public static function validateStartEndDatepickerInputs($startFormKey, $startValue, $endFormKey, $endValue) {
+
+ // Check date as well as time is set
+ if (!empty($startValue) && !self::datePickerValueWithTimeHasDate($startValue)) {
+ return ['key' => $startFormKey, 'message' => ts('Please enter a date as well as a time.')];
+ }
+ if (!empty($endValue) && !self::datePickerValueWithTimeHasDate($endValue)) {
+ return ['key' => $endFormKey, 'message' => ts('Please enter a date as well as a time.')];
+ }
+
+ // Check end date is after start date
+ if (!empty($startValue) && !empty($endValue) && $endValue < $startValue) {
+ return ['key' => $endFormKey, 'message' => ts('The end date should be after the start date.')];
+ }
+
+ return TRUE;
+ }
+