ursus contionabundo: Datum kürzen

Beitrag lesen

Kleine Verbesserung bzgl. "Fehlerfestigkeit".

<?php
function gerShortDate( $s = false ) {
   if ( preg_match('/[^0-9\/.-]/', $s ) ) {
	    trigger_error(
	      "function date2german: Typ für '$s' nicht implementiert.",
          E_USER_NOTICE
        );
        return false;
   }
   if ( false === $s )  {
		 return date( 'd.m.y' );
   }
   if ( 'integer' == gettype( $s ) ) {
     return date( 'd.m.y', $s );
   } else {
	   if ( strpos( $s, '.' ) ) {
		     $a  = explode ('.', $s );
		     $a[2] = $a[2] % 100;
		     $s = implode('/', [ $a[1], $a[0], $a[2] ] );
     } 
     return date( 'd.m.y', strtotime( $s ) );
   }
}

function test($input=false)
{
   echo sprintf("% 12s", $input),"\t", gerShortDate($input) , "\n";
}

test();
test(time());
test('7. Mai 2019');
# Deutsch
test("7.5.9");
test("7.5.19");
test("07.05.19");
test("07.05.219");
test("07.05.2019");
# Englisch:
test("5/7/9");
test("05/07/19");
test("05/07/219");
test("5/7/2019");
test("05/07/2019");
# ISO:
test("19-05-7");
test("19-5-7");
test("2019-5-07");
test("9-05-07");
test("19-05-07");
test("219-05-07");
test("2019-05-07");

Resultate:


            	07.05.19
  1557222454	07.05.19
 7. Mai 2019	PHP Notice:  function date2german: Typ für '7. Mai 2019' nicht implementiert. in /tmp/test.php on line 6
       7.5.9	07.05.09
      7.5.19	07.05.19
    07.05.19	07.05.19
   07.05.219	07.05.19
  07.05.2019	07.05.19
       5/7/9	07.05.09
    05/07/19	07.05.19
   05/07/219	07.05.19
    5/7/2019	07.05.19
  05/07/2019	07.05.19
     19-05-7	07.05.19
      19-5-7	07.05.19
   2019-5-07	07.05.19
     9-05-07	07.05.09
    19-05-07	07.05.19
   219-05-07	07.05.19
  2019-05-07	07.05.19
0 44

Datum kürzen

Bernd
  • php
  1. 1
    dedlfix
    1. 0
      Bernd
      1. 0
        dedlfix
  2. 0
    Wallpappe
  3. 0
    ursus contionabundo
    1. 0
      Rolf B
      1. 0
        dedlfix
      2. 0
        ursus contionabundo
        1. 0
          ursus contionabundo
        2. 0
          dedlfix
          1. 0
            ursus contionabundo
            1. 0
              dedlfix
              1. 0
                1unitedpower
                1. 0
                  ursus contionabundo
                  1. 0
                    dedlfix
                    1. 0
                      ursus contionabundo
                      1. 0
                        dedlfix
                        1. 0
                          ursus contionabundo
                  2. 1
                    1unitedpower
                    1. 0
                      ursus contionabundo
                      1. 0
                        dedlfix
                        1. 0
                          ursus contionabundo
                          1. 0
                            Tabellenkalk
                            1. 0
                              ursus contionabundo
                            2. 0

                              microtime(true)

                              ursus contionabundo
                              1. 0
                                dedlfix
                                1. 0
                                  ursus contionabundo
                                  1. 0
                                    Matthias Apsel
                                    • sonstiges
                                    1. 5
                                      MudGuard
                              2. 0
                                ursus contionabundo
                              3. 0
                                Tabellenkalk
                                1. -1

                                  Kann passieren

                                  ursus contionabundo
                          2. 0
                            dedlfix
                            1. 0
                              ursus contionabundo
                      2. 0
                        Tabellenkalk
                        1. 0
                          ursus contionabundo
                      3. 0
                        1unitedpower
                        1. 0
                          ursus contionabundo
                          1. 0
                            1unitedpower
                            1. 0

                              PS: Benchmark Code

                              1unitedpower
                            2. 0
                              Rolf B
                              1. 0
                                1unitedpower
      3. 0
        ursus contionabundo