/** * Caller * * @param mixed $date Unix timestamp or string, e.g '+ 1 week' * * @return string */ public function age($date) { if (Value::isEmpty($date)) { return null; } $date = Value::getDate($date); $now = new \DateTime('now'); $age = $now->diff($date)->y; return $age; }
/** * {@inheritdoc} */ public function validate($values) { if (!is_array($values) || count($values) !== 2) { return false; } if (Value::isEmpty($values[0]) && Value::isEmpty($values[1])) { return true; } $left = Value::getDate($values[0]); $right = Value::getDate($values[1]); $between = new DateBetween($left, $right); $flag = $between->validate($this->left) || $between->validate($this->right); return $flag; }
/** * Caller * * @param \DateTime|mixed $value Unix timestamp or string, e.g '+ 1 week' * @param string $format Date format * * @return string * @throws \InvalidArgumentException */ public function date($value = null, $format = '%Y-%m-%d %H:%M:%S') { if ($value === null) { $value = new \DateTime(); } else { if (Value::isEmpty($value)) { return null; } $value = Value::getDate($value); if ($value === null) { return null; } } $result = strftime($format, $value->getTimestamp()); return $result; }
/** * {@inheritdoc} */ public function validate($value) { if (Value::isEmpty($value)) { return true; } $value = Value::getDate($value); if ($value === null) { return false; } $v = $value->getTimestamp(); $l = $this->left === null ? -INF : $this->left; $r = $this->right === null ? INF : $this->right; $flag = null; if ($this->edges) { $flag = $v - $l >= 0 && $v - $r <= 0 || $v - $l <= 0 && $v - $r >= 0; } else { $flag = $v - $l > 0 && $v - $r < 0 || $v - $l < 0 && $v - $r > 0; } return $flag; }
/** * {@inheritdoc} */ public function filter($value) { $value = Value::getDate($value); return $value; }