Snippets tagged "date" Snippets tagged "date"

Convertir un timestamp unix en ligne de commande

C'est bête mais ça peut toujours servir.

$ date -r 1204748227
Wed Mar  5 12:17:07 PST 2008
by Nicolas Perriault on 2008-04-18, tagged cli  date  time 

[Symfony] Un validateur de date qu'il est bien

Quelques fonctionnalités en plus par rapport à sfDateValidator (qu'il étend), notamment l'interdiction de date futures ou passées et l'évitement du traitement de la langue courante utilisateur (utile pour les formats de date figés).

/**
 * Custom date validator
 *
 */
class myDateValidator extends sfDateValidator
{
  /**
   * Execute this validator.
   *
   * @param mixed A file or parameter value/array
   * @param error An error message reference
   *
   * @return bool true, if this validator executes successfully, otherwise false
   */
  public function execute(&$value, &$error)
  {
    $culture = $this->getContext()->getUser()->getCulture();
 
    // Validate the given date
    if ($this->getParameter('with_culture'))
    {
      $value1 = $this->getValidDate($value, $culture);
    }
    else
    {
      $value1 = strtotime($value);
    }
 
    if (!$value1 || -1 == $value1) // Before php 5.1, strtotime() returns -1 on fail
    {
      $error = $this->getParameter('date_error');
      return false;
    }
 
    // Is there a compare to do?
    $compareDateParam = $this->getParameter('compare');
    $compareDate = $this->getContext()->getRequest()->getParameter($compareDateParam);
 
    // If the compare date is given
    if ($compareDate)
    {
      $operator = trim($this->getParameter('operator', '=='), '\'" ');
      $value2 = $this->getValidDate($compareDate, $culture);
 
      // If the check date is valid, compare it. Otherwise ignore the comparison
      if ($value2)
      {
        $valid = false;
        switch ($operator)
        {
          case '>':
            $valid = $value1 >  $value2;
            break;
          case '>=':
            $valid = $value1 >= $value2;
            break;
          case '==':
            $valid = $value1 == $value2;
            break;
          case '<=':
            $valid = $value1 <= $value2;
            break;
          case '<':
            $valid = $value1 <  $value2;
            break;
 
          default:
            throw new sfValidatorException(sprintf('Invalid date comparison operator "%s"', $operator));
        }
 
        if (!$valid)
        {
          $error = $this->getParameter('compare_error');
 
          return false;
        }
      }
    }
 
    if (!$this->getParameter('allow_future') && $value1 > time())
    {
      $error = $this->getParameter('future_error');
      return false;
    }
 
    if (!$this->getParameter('allow_past') && $value1 < time())
    {
      $error = $this->getParameter('past_error');
      return false;
    }
 
    return true;
  }
 
  /**
   * Initializes the validator.
   *
   * @param sfContext The current application context
   * @param array   An associative array of initialization parameters
   *
   * @return bool true, if initialization completes successfully, otherwise false
   */
  public function initialize($context, $parameters = null)
  {
    // Initialize parent
    parent::initialize($context, $parameters);
 
    // Set defaults
    $this->getParameterHolder()->set('with_culture', true);
    $this->getParameterHolder()->set('allow_future', true);
    $this->getParameterHolder()->set('future_error', 'Future dates not allowed');
    $this->getParameterHolder()->set('allow_past', true);
    $this->getParameterHolder()->set('past_error', 'Past dates not allowed');
    $this->getParameterHolder()->add($parameters);
 
    return true;
  }
}
by Nicolas Perriault on 2008-03-16, tagged date  php  symfony  validator