<?php
class Pampa_Util_Date {

  static function dateTimeToSql($dateTime, $sql = TRUE, $type = 'all')
  {
    $date = explode(' ', $dateTime);

    switch($type)
    {
      case 'time':
        return $date[1];
        break;
      case 'date':
        if($sql)
        {
          // d/m/Y -> Y-m-d
          return implode('-', array_reverse(explode('/', $date[0])));
        }
        else
        {
          // Y-m-d -> d/m/Y
          return implode('/', array_reverse(explode('-', $date[0])));
        }
        break;
      case 'all':
        if($sql)
        {
          // d/m/Y -> Y-m-d
          return implode('-', array_reverse(explode('/', $date[0]))).' '.$date[1];
        }
        else
        {
          // Y-m-d -> d/m/Y
          return implode('/', array_reverse(explode('-', $date[0]))).' '.$date[1];
        }
        break;
    }
  }

  static function sqlDate($date, $to_sql = TRUE) {
      if(empty($date)){
        return NULL;
      }

      if($to_sql)
      {
        $separator = '/';
      }
      else
      {
        $separator = '-';
      }

      // explode all params
      $date_parts = explode($separator, $date);

      // check for way to divide
      if(!$to_sql){
        $params = array(
          "year" => $date_parts[0],
          "month" => $date_parts[1],
          "day" => $date_parts[2]
        );
      } else {
        $params = array(
          "year" => $date_parts[2],
          "month" => $date_parts[0],
          "day" => $date_parts[1]
        );
      }
      $zendDate = new Zend_Date($params);

      if($to_sql) {
        $newDate = date("Y-m-d", $zendDate->get(Zend_Date::TIMESTAMP));
      } else {
        $newDate = date("m/d/Y", $zendDate->get(Zend_Date::TIMESTAMP));
      }
      // VALIDA SE FOI PASSADO DADO NULO
      if(is_null($date) || $date == "") {
        return "-";
      }
      return $newDate;
    }
  /**
   * Converte o formato mm-dd-YYYY para dd/mm/YYY
   * exemplo: 01-12-2001 para 12/01/2001
   * @param <String> $date
   * @param <String> $split_separator
   * @param <String> $return_separator
   * @return <String>
   */
  static function switchDate($date, $split_separator = "-", $return_separator = "/"){
    $a = explode($split_separator, $date);
    return $a[1].$return_separator.$a[0].$return_separator.$a[2];
  }

  static function toTimestamp($date){

        $date_array = explode("-", $date); // split the array
        $var_year = $date_array[0];
        $var_month = $date_array[1];
        $var_day = $date_array[2];

        // cria a data em zend
        /**
        $zendDate = new Zend_Date(array(
          "year" => $var_year,
          "month" => $var_month,
          "day" => $var_day
        ));

        return $zendDate->get(Zend_Date::W3C);
        /**/

        $var_timestamp = mktime(0,0,0,$var_month,$var_day,$var_year);
        return($var_timestamp); // return it to the user

  }

  /**
   * Retorna o time a partir de timestamp
   * do MySQL
   * @param String $date
   * @return Date
   */
  public static function getTimestamp($date) {
    $zendDate = new Zend_Date();
    $zendDate->set($date, Zend_Date::ISO_8601);

    return $zendDate->get();
  }

  /**
   * Retorna uma data no formato Timestamp
   * do MySQL
   * @param String $date
   * @param Boolean $sql
   * @return String
   */
  public static function dateToTimestamp($date, $sql = TRUE){
    // se a data nao estiver em formato MySQL, converte
    if(!$sql){
      $date = self::sqlDate($date, true);
    }
    return "{$date} 00:00:01";
  }

  /**
   * convert a date with the given format
   * @param String $source date format
   * @param String $date date object/string
   * @param String $source type of format
   * @param Array|String $params extra params
   * @return String
   */
  public static function date($format = 'd/m/Y', $date = NULL, $source = 'from_mysql_timestamp', $params = NULL){
    if(empty($date)){
      $date = date($format);
    }
    $zendDate = self::toZendDate($date, $source, $params);
    return date($format, $zendDate->get());
  }

  /**
   * returns a zend_date object from the given format type
   * @param String $date the date to be converted
   * @param String $source what kind of conversion
   * @param Array|String $params extra params
   * @return NULL|Zend_Date
   */
  public static function toZendDate($date = '', $source = 'from_mysql_timestamp', $params = NULL){
    switch ($source) {
      case 'from_mysql_timestamp':
        $zendDate = new Zend_Date($date, Zend_Date::ISO_8601);
        break;
      case 'from_mysql_date':
        // explode date to get parts
        $date_parts = explode("-", $date);

        // setup date params
        $params = array(
          "year" => $date_parts[0],
          "month" => $date_parts[1],
          "day" => $date_parts[2]
        );

        // creates the zend_date object
        $zendDate = new Zend_Date($params);
        break;
      
      case 'from_text_input':
        // check for extra params
        if($params['input_format'] == 'd/m/y'){
          
          // explode date to get parts
          $date_parts = explode("/", $date);
          
          // setup date params
          $params = array(
            "year" => $date_parts[2],
            "month" => $date_parts[1],
            "day" => $date_parts[0]
          );
        }

        // creates the zend_date object
        $zendDate = new Zend_Date($params);
        break;
      default:
        throw new Exception("Invalid format date");
        break;
    }

    return $zendDate;
  }

  /**
   * convert a date string to mysql date string
   * @param String $date date input
   * @param String $input_format how the input was formatted
   * @return String
   */
  public static function toMysqlDate($date = '', $input_format = 'd/m/y'){
    $date = self::date('Y-m-d', $date, 'from_text_input', array('input_format' => $input_format));
    return $date;
  }
}