Location: PHPKode > scripts > trDate > trdate/trDate.class.php
<?php

/*
SRO_Admin - Access file class
Copyright (C) 2005  Clauzio C. Perpétuo

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

/**
 * Translate date format
 *
 * Translate date number format
 * @package trDate
 * @author Clauzio C. Perpétuo
 * @version 0.1
 * @copyright licence above
 */
class trDate {

  /**
   * get trDate
   * @access static public
   * @param string $in
   * @param string $out
   * @param string $date
   */
  static public function get ($in, $out, $date)
  {

    $reg = '^([-/\ \.Yymndj]{8,10})$';
    if (!ereg($reg, $in.$out))
      return 1;

    $date = ereg_replace('[/\ \.]', '-', $date);
    $_date = explode('-', $date);
    $date = sprintf('%02u', $_date[0])
      . sprintf('%02u', $_date[1])
      . sprintf('%02u', $_date[2]);

    $in = ereg_replace('[/\ \.-]', '', $in);

    for ($i=0; $i<strlen($in); $i++) {
      $v = $in[$i];

      if (ereg('[Y]', $v))
        $len = 4;
      elseif (ereg('[ymndj]', $v))
        $len = 2;

      $dt[$v] = substr($date, 0, $len);
      $date = substr($date, $len);

      if (ereg('[Yy]', $v))
        $v == 'y' ?
          $dt['Y'] = ($dt[$v] <= date($v) ? '20'.$dt[$v] : '19'.$dt[$v]) :
          $dt['y'] = substr($dt[$v], 2);
      elseif (ereg('[mn]', $v))
        $v == 'n' ?
          $dt['m'] = $dt[$v] :
          $dt['n'] = (int) $dt[$v];
      elseif (ereg('[dj]', $v))
        $v == 'j' ?
          $dt['d'] = $dt[$v] :
          $dt['j'] = (int) $dt[$v];
    }

    $j = 0; $o_dt = $sp = array();
    for ($i=0; $i<strlen($out); $i++) {
      $v = $out[$i];

      if (!ereg('[Yymndj]', $v))
        $sp[$j++] = $v;
      else
        $o_dt[] = $dt[$v];
    }

    $ret = $o_dt[0]
      . @$sp[0]
      . $o_dt[1]
      . @$sp[1]
      . $o_dt[2];

    return $ret;
  }
}


// ONLY TEST

$date = '23.4.75';
echo trDate::get('j.n.y', 'Y-m-d', $date)."\n"; //1975-04-23

// END TEST

?>
Return current item: trDate