Location: PHPKode > scripts > germandate > germandate/germandate.php
<?php
/*
* Filename.....: germandate.php
* Features.....: Output a german formatted datestring using
*                standard date() formatting rules
*
* Erstellt am..: Dienstag, 18. Februar 2003 07:38:00
*       _  __      _ _
*  ||| | |/ /     (_) |        Wirtschaftsinformatiker IHK
* \. ./| ' / _ __  _| |_ ___   www.ingoknito.de
* - ^ -|  < | '_ \| | __/ _ \
* / - \| . \| | | | | || (_) | Peter Klauer
*  ||| |_|\_\_| |_|_|\__\___/  06131-651236
* mailto.......: hide@address.com
*
* Purpose:
* Replace the D, l(small L), M, F in a date() format string with
* the german counterparts. German weekdays are only 2 chars long,
* when shown in short format.
*
* D - Tag der Woche als 2 Buchstaben, z.B. "Fr"
* l - (kleines 'L') ausgeschriebener Tag der Woche, z.B. "Freitag"
* M - Monat als 3 Buchstaben, z.B. "Jan"
* F - Monat als ganzes Wort, z.B "Januar"
*
* example:
*
*     $gd = new germandate('D, d. M Y');
*     echo $gd->date();
*
* Changes:
* 2005-12-05: NEW: function mktime_from_iso() for iso dates which drop out
*             of a sql table.
*             New: parameter $windows to stop dates before 1970
*/

class germandate
{

  var $format = 'D., d. F Y';
  var $mktime;
  var $windows = false;

  var $monate = array(
  'Januar','Februar','März','April','Mai','Juni',
  'Juli','August','September','Oktober','November','Dezember');

  var $tage = array('Sonntag','Montag','Dienstag',
  'Mittwoch','Donnerstag','Freitag','Samstag');

  var $debug = false;

  /**
  * Class constructor
  * @param string $format = Date format string like in standard "date()" function
  * @param integer $mktime = Date value as returned by standard "mktime()" function
  * @param boolean $windows = does it run on windows?
  * @return void 
  */
  function germandate( $format = '', $mktime = '', $windows = false )
  {

    if( strlen( $format ) > 0 ) $this->format = $format;
    if( strlen( $mktime ) > 0 )
    {
      $this->mktime  = $mktime;
    }
    else
    {
      $this->mktime = mktime();
    }
    $this->windows = $windows;

  } // end of constructor germandate()


  /**
  * Returns german formatted datestring
  * Instead of using the standard date() function, you
  * use germandate::date() to format a date.
  * @param string $format = Date format string like in standard "date()" function
  * @param integer $mktime = Date value as returned by standard "mktime()" function
  * @return string 
  */
  function date($format = '', $mktime = '')
  {
    if( strlen( $format ) > 0 ) $this->format = $format;
    if( strlen( $mktime ) > 0 )
    {
      $this->mktime  = $mktime;
    }
    else
    {
      $this->mktime = mktime();
    }

    $gerstring = ''; // return value

    if( $this->debug )
    {
      echo '<br><br>$format = '.$this->format.'<br>';
      echo '$itime  = '.$this->mktime.'<br>';
      echo '$windows = '.$this->windows.'<br>';

    }

    $ok = true; // we are on a ??ix system
    if( $this->windows and ($mktime < 1 ))
    {
      $ok = false;
    }

    if( $ok )
    {

      $fger = str_replace( 'D','@1', $this->format ); // $tag3
      $fger = str_replace( 'l','@2', $fger ); // $tag
      $fger = str_replace( 'M','@3', $fger ); // $monat3
      $fger = str_replace( 'F','@4', $fger ); // $monat

      # Tagstrings
      $tag   = $this->tage[ date( 'w', $this->mktime )] ;
      $tag3  = substr( $tag, 0, 2 );

      # Monatstrings
      $monat = $this->monate[ date( 'm', $this->mktime ) -1] ;
      $monat3= substr( $monat, 0, 3 );

      # Wochentagname ersetzten
      $gerstring = str_replace( '@1',$tag3, date( $fger, $this->mktime ) );
      $gerstring = str_replace( '@2',$tag, $gerstring );
      # Monatsname ersetzen
      $gerstring = str_replace( '@3',$monat3, $gerstring );
      $gerstring = str_replace( '@4',$monat, $gerstring );

      if( $this->debug )
      {
        echo 'germanmask='.date( $fger, $this->mktime ).'<br>';
        $original = date( $this->format, $this->mktime );
        echo "original  =$original<br>";
        echo "German Strings: $tag $tag3 $monat $monat3 <br>";
        echo "gerstring = $gerstring<br>";
      }
    }
    return( $gerstring );

  } // end of function date()


  /**
  * Use this to format date values which come out of a database.
  * Mysql returns directly an iso date with "select <datecolumn> ..."
  * Mssql needs a little help in the query: "select convert( varchar, <datecolumn>, 23 ) as <datecolumn> ..."
  * (Note: Mssql iso date including the time value is 25 instead of 23)
  *
  * @param string $isodate = String formatted like YYYY-MM-DD with leading zeroes.
  * @return integer mktime-value (unix timestamp)
  */
  function mktime_from_iso( $isodate )
  {

    # isodate today = 2005-04-24 (sunday, 24 april 2005)
    # use mktime to generate a serialdate for germandate
    return mktime( 12, 0, 0, substr( $isodate, 5,2 ),substr($isodate,8,2),substr($isodate,0,4));

  } // end of function mktime_from_iso()

} // end of class germandate
?>
Return current item: germandate