Autor Thema: Mal wieder ne PHP / MySQL Frage...  (Gelesen 1712 mal)

0 Mitglieder und 1 Gast betrachten dieses Thema.

Offline Karl Lauer

  • Alte Garde
  • Legend
  • *******
  • Back 2 the roots
  • Beiträge: 6.518
  • Geschlecht: Männlich
  • Username: Karl Lauer
Mal wieder ne PHP / MySQL Frage...
« am: 2.01.2007 | 14:24 »
Also ich hab da so ne SQL Datenbank, ne. Da gibt es eine Tabelle, die ein "date" Feld hat. Die Daten da drin sehen ja z.B. so aus : '2007-01-02'

Wie mach ich das jetzt am dümmsten, wenn ich in meinem PHP Programm das Feld gerne auslesen und mit strftime formatiert ausgeben möchte?

Steh da gedanklich gerade auf einem Schlauch...
"When I was a kid... I dreamed of outer space. And then I got here - and I dream of Earth" John Crichton, Farscape
“You need to get yourself a better dictionary. When you do, look up “genocide”. You’ll find a little picture of me there, and the caption’ll read “Over my dead body.” Tenth Doctor Who

Offline Thalamus Grondak

  • Mythos
  • ********
  • Beiträge: 9.220
  • Geschlecht: Männlich
  • Username: Thalamus Grondak
Re: Mal wieder ne PHP / MySQL Frage...
« Antwort #1 am: 2.01.2007 | 14:34 »
Hast du schonmal eine DB-Abfrage gemacht?
Even if you win the Rat race, you´re still a Rat

Offline Karl Lauer

  • Alte Garde
  • Legend
  • *******
  • Back 2 the roots
  • Beiträge: 6.518
  • Geschlecht: Männlich
  • Username: Karl Lauer
Re: Mal wieder ne PHP / MySQL Frage...
« Antwort #2 am: 2.01.2007 | 14:44 »
Ja. Mein Problem ist (weil ich es nicht besser weiss), das das ausgelesene date Feld wohl als string ausgelesen wird und nicht als UNIX-timestamp. So hab ich einen string den ich nicht einfach mit strftime formatieren kann und einen befehl mit dem ich das ganze umwandle kenn ich halt nicht...

(sorry für die kurzen evtl. unvollständigen Posts... ist nebenher @work getippt ::) )
"When I was a kid... I dreamed of outer space. And then I got here - and I dream of Earth" John Crichton, Farscape
“You need to get yourself a better dictionary. When you do, look up “genocide”. You’ll find a little picture of me there, and the caption’ll read “Over my dead body.” Tenth Doctor Who

Offline Thalamus Grondak

  • Mythos
  • ********
  • Beiträge: 9.220
  • Geschlecht: Männlich
  • Username: Thalamus Grondak
Re: Mal wieder ne PHP / MySQL Frage...
« Antwort #3 am: 2.01.2007 | 14:49 »
Hiermit wandelst du einen (fast) beliebigen Datumsstring in unixtime um.
function parse_date($strDate)
{
if($strDate != "")
{
$month = array( 'januar' => 1,
                'februar' => 2,
    'märz' => 3,
                'april' => 4,
                'mai' => 5,
        'juni' => 6,
        'july' => 7,
        'august' => 8,
        'september' => 9,
                'oktober' => 10,
        'november' => 11,
        'dezember' => 12 );
  // funtion tryes to interpret date and returns unixtime.
  // if interpreting fails, it return -1
  $strDate = strtolower($strDate);
  $strDate = trim($strDate);
  $response = -1;
   // *** ISO 8601 - 1970-09-17
   if (preg_match("/^(\d{2,4})-(\d{1,2})-(\d{1,2})$/",$strDate, $regex))
   {
      if(checkdate($regex[2], $regex[3], $regex[1]))
      $response = mktime(0,0,0,$regex[2], $regex[3], $regex[1]);
   }
   // with time
   if (preg_match("/^(\d{2,4})-(\d{1,2})-(\d{1,2}) (\d{1,2}):(\d{1,2})$/",$strDate, $regex))
   {
if(checkdate($regex[2], $regex[3], $regex[1]))
$response = mktime($regex[4],$regex[5],0,$regex[2], $regex[3], $regex[1]);
   }
   // *** german format - 17.09.1970 and 17.09.70
   elseif (preg_match("/^(\d{1,2}).(\d{1,2}).(\d{2,4})$/",$strDate, $regex))
   {
if(checkdate($regex[2], $regex[1], $regex[3]))
$response = mktime(0,0,0,$regex[2], $regex[1], $regex[3]);
   }
   // with time
   elseif (preg_match("/^(\d{1,2}).(\d{1,2}).(\d{2,4}) (\d{1,2}):(\d{1,2})$/",$strDate, $regex))
   {
    if(checkdate($regex[2], $regex[1], $regex[3]))
    $response = mktime($regex[4], $regex[5],0,$regex[2], $regex[1], $regex[3]);
   }
   // * U.S. writeing - 9/17/72
   elseif (preg_match("/^(\d{1,2}).(\d{1,2}).(\d{2,4})$/",$strDate, $regex))
   {
    if(checkdate($regex[1], $regex[2], $regex[3]))
      $response = mktime(0,0,0,$regex[1], $regex[2], $regex[3]);
   }
   // with time
   elseif (preg_match("/^(\d{1,2}).(\d{1,2}).(\d{2,4}) (\d{1,2}):(\d{1,2})$/",$strDate, $regex))
   {
      if(checkdate($regex[1], $regex[2], $regex[3]))
      $response = mktime($regex[4],$regex[5],0,$regex[1], $regex[2], $regex[3]);
   }
   // * "2k format" - 23.11.2k2
   elseif (preg_match("/^(\d{1,2}).(\d{1,2}).(\dk\d)$/",$strDate, $regex))
   {
      $regex[3] = str_replace("k", "00", $regex[3]);
      if(checkdate($regex[2], $regex[1], $regex[3]))
      $response = mktime(0,0,0,$regex[2], $regex[1], $regex[3]);
   }
   // with time
   elseif (preg_match("/^(\d{1,2}).(\d{1,2}).(\dk\d) (\d{1,2}):(\d{1,2})$/",$strDate, $regex))
   {
      $regex[3] = str_replace("k", "00", $regex[3]);
      if(checkdate($regex[2], $regex[1], $regex[3]))
      $response = mktime($regex[4],$regex[5],0,$regex[2], $regex[1], $regex[3]);
   }
   // standard-format 23 Mai 2002, 13:30:30 (GMT)
   elseif (preg_match("/^(\d{1,2}) ([\w|ä|ö|ü]*?) (\d{2,4})\, (\d{1,2}):(\d{1,2}):(\d{1,2}) \((\w*?)\)/",$strDate, $regex))
   {
      if(empty($month[$regex[2]])) print_r ($regex);
      $regex[2] = $month[$regex[2]];
    if(checkdate($regex[2], $regex[1], $regex[3]))
      $response = mktime($regex[4],$regex[5],$regex[6], $regex[2], $regex[1], $regex[3]);
   }
   // * today or heute
   elseif ($strDate == 'heute' or $strDate == 'today')
   {
      $response = mktime(0,0,0,sds_date("m"), sds_date("d"), sds_date("Y"));
   }
   // * now or jetzt
     elseif ($strDate == 'now' or $strDate == 'jetzt')
   {
    $response = time();
    }
}
else
{
    $response = "";
}
   return $response;
}
Even if you win the Rat race, you´re still a Rat

Ein

  • Gast
Re: Mal wieder ne PHP / MySQL Frage...
« Antwort #4 am: 2.01.2007 | 14:57 »
Probier es mal, das Datum als SELECT UNIX_TIMESTAMP auszulesen. vgl. http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html

Offline Karl Lauer

  • Alte Garde
  • Legend
  • *******
  • Back 2 the roots
  • Beiträge: 6.518
  • Geschlecht: Männlich
  • Username: Karl Lauer
Re: Mal wieder ne PHP / MySQL Frage...
« Antwort #5 am: 2.01.2007 | 15:05 »
Ich hatte ja auf einen Befehl gehofft, der mir bis jetzt entgangen ist.. aber dann häng ich am besten die function noch in meine include Datei...

Danke

Edit:
Danke Sven, das versuch ich zuerst mal... :)
"When I was a kid... I dreamed of outer space. And then I got here - and I dream of Earth" John Crichton, Farscape
“You need to get yourself a better dictionary. When you do, look up “genocide”. You’ll find a little picture of me there, and the caption’ll read “Over my dead body.” Tenth Doctor Who

Offline Karl Lauer

  • Alte Garde
  • Legend
  • *******
  • Back 2 the roots
  • Beiträge: 6.518
  • Geschlecht: Männlich
  • Username: Karl Lauer
Re: Mal wieder ne PHP / MySQL Frage...
« Antwort #6 am: 3.01.2007 | 10:14 »
So nach durchforsten der MYSQL Referenz hab ich endlich den Befehl gefunden, den ich gesucht hatte :)

Mir ging es nämlich erst mal nur um die formatierte Ausgabe und da langt es mit SELECT DATE_FORMAT() zu arbeiten :)


Danke nochmal für die Tipps
"When I was a kid... I dreamed of outer space. And then I got here - and I dream of Earth" John Crichton, Farscape
“You need to get yourself a better dictionary. When you do, look up “genocide”. You’ll find a little picture of me there, and the caption’ll read “Over my dead body.” Tenth Doctor Who

Offline Bombshell

  • Zonen-Stefan
  • Helfer
  • Legend
  • ******
  • Beiträge: 5.620
  • Geschlecht: Männlich
  • Username: Bombshell
Re: Mal wieder ne PHP / MySQL Frage...
« Antwort #7 am: 3.01.2007 | 11:51 »
Hallo,

Hiermit wandelst du einen (fast) beliebigen Datumsstring in unixtime um.
...

Dir war wohl strtotime zu einfach?

MfG

Stefan
Kurze klare Worte