diff --git a/locales/es_ES/translations.php b/locales/es_ES/translations.php
index bf6f80e6d..4c036f002 100644
--- a/locales/es_ES/translations.php
+++ b/locales/es_ES/translations.php
@@ -289,4 +289,5 @@ return array(
// 'Nothing found.' => '',
// 'Search in the project "%s"' => '',
// 'Due date' => '',
+ // 'Others formats accepted: %s and %s' => '',
);
diff --git a/locales/fr_FR/translations.php b/locales/fr_FR/translations.php
index 54659276f..82467acd8 100644
--- a/locales/fr_FR/translations.php
+++ b/locales/fr_FR/translations.php
@@ -289,4 +289,5 @@ return array(
'Nothing found.' => 'Rien trouvé.',
'Search in the project "%s"' => 'Rechercher dans le projet « %s »',
'Due date' => 'Date d\'échéance',
+ 'Others formats accepted: %s and %s' => 'Autres formats acceptés : %s et %s',
);
diff --git a/locales/pl_PL/translations.php b/locales/pl_PL/translations.php
index ed16779fe..a1be50d31 100644
--- a/locales/pl_PL/translations.php
+++ b/locales/pl_PL/translations.php
@@ -294,4 +294,5 @@ return array(
// 'Nothing found.' => '',
// 'Search in the project "%s"' => '',
// 'Due date' => '',
+ // 'Others formats accepted: %s and %s' => '',
);
diff --git a/locales/pt_BR/translations.php b/locales/pt_BR/translations.php
index 54b357db9..54538b1ca 100644
--- a/locales/pt_BR/translations.php
+++ b/locales/pt_BR/translations.php
@@ -290,4 +290,5 @@ return array(
// 'Nothing found.' => '',
// 'Search in the project "%s"' => '',
// 'Due date' => '',
+ // 'Others formats accepted: %s and %s' => '',
);
diff --git a/models/task.php b/models/task.php
index bef92f207..f08f0b288 100644
--- a/models/task.php
+++ b/models/task.php
@@ -6,6 +6,7 @@ require_once __DIR__.'/base.php';
use \SimpleValidator\Validator;
use \SimpleValidator\Validators;
+use DateTime;
/**
* Task model
@@ -301,7 +302,7 @@ class Task extends Base
}
if (! empty($values['date_due']) && ! is_numeric($values['date_due'])) {
- $values['date_due'] = $this->getTimestampFromDate($values['date_due'], t('m/d/Y')) ?: null;
+ $values['date_due'] = $this->parseDate($values['date_due']);
}
$values['date_creation'] = time();
@@ -334,7 +335,7 @@ class Task extends Base
{
// Prepare data
if (! empty($values['date_due']) && ! is_numeric($values['date_due'])) {
- $values['date_due'] = $this->getTimestampFromDate($values['date_due'], t('m/d/Y')) ?: null;
+ $values['date_due'] = $this->parseDate($values['date_due']);
}
$original_task = $this->getById($values['id']);
@@ -472,7 +473,7 @@ class Task extends Base
new Validators\Integer('score', t('This value must be an integer')),
new Validators\Required('title', t('The title is required')),
new Validators\MaxLength('title', t('The maximum length is %d characters', 200), 200),
- new Validators\Date('date_due', t('Invalid date'), t('m/d/Y')),
+ new Validators\Date('date_due', t('Invalid date'), $this->getDateFormats()),
));
return array(
@@ -523,7 +524,7 @@ class Task extends Base
new Validators\Integer('score', t('This value must be an integer')),
new Validators\Required('title', t('The title is required')),
new Validators\MaxLength('title', t('The maximum length is %d characters', 200), 200),
- new Validators\Date('date_due', t('Invalid date'), t('m/d/Y')),
+ new Validators\Date('date_due', t('Invalid date'), $this->getDateFormats()),
));
return array(
@@ -557,19 +558,19 @@ class Task extends Base
}
/**
- * Parse a date (different format for each locale) to a timestamp
+ * Return a timestamp if the given date format is correct otherwise return 0
*
* @access public
* @param string $value Date to parse
* @param string $format Date format
* @return integer
*/
- public function getTimestampFromDate($value, $format)
+ public function getValidDate($value, $format)
{
- $date = \DateTime::createFromFormat($format, $value);
+ $date = DateTime::createFromFormat($format, $value);
if ($date !== false) {
- $errors = \DateTime::getLastErrors();
+ $errors = DateTime::getLastErrors();
if ($errors['error_count'] === 0 && $errors['warning_count'] === 0) {
$timestamp = $date->getTimestamp();
return $timestamp > 0 ? $timestamp : 0;
@@ -578,4 +579,40 @@ class Task extends Base
return 0;
}
+
+ /**
+ * Parse a date ad return a unix timestamp, try different date formats
+ *
+ * @access public
+ * @param string $value Date to parse
+ * @return integer
+ */
+ public function parseDate($value)
+ {
+ foreach ($this->getDateFormats() as $format) {
+
+ $timestamp = $this->getValidDate($value, $format);
+
+ if ($timestamp !== 0) {
+ return $timestamp;
+ }
+ }
+
+ return null;
+ }
+
+ /**
+ * Return the list of supported date formats
+ *
+ * @access public
+ * @return array
+ */
+ public function getDateFormats()
+ {
+ return array(
+ t('m/d/Y'),
+ 'Y-m-d',
+ 'Y_m_d',
+ );
+ }
}
diff --git a/templates/task_edit.php b/templates/task_edit.php
index 856f7d5d8..9fe0418fc 100644
--- a/templates/task_edit.php
+++ b/templates/task_edit.php
@@ -25,6 +25,7 @@
= Helper\form_label(t('Due Date'), 'date_due') ?>
= Helper\form_text('date_due', $values, $errors, array('placeholder="'.t('month/day/year').'"'), 'form-date') ?>
+