2017-05-11 2 views
-1

Ich bin sehr neu mit Laravel und ich habe ein Problem mit einer Spalte, die ich in den Migrationen erstellt habe. Ich habe eine Spalte vom Typ date. Mein Problem ist, dass ich nicht weiß, wie kann ich es verwalten, wie muss ich einige Daten für ein Formular usw. speichern.Wie verwende ich DATE mit Laravel PHP

Ich fragte, ob jemand mir sagen könnte, in einer Methode eines Controllers mit der Parameter Resquest $ r, wie kann man die Zeichenkette aus diesem Formular in eine Variable von date umwandeln. Ich habe gesucht und ich kann nichts finden, was mir helfen könnte. Danke im Voraus!

Edit:

Dies ist eine Methode meiner Controller:

public function addObjectives(Request $r,$id){ 

    $gobjectives = Mgobjective::all(); 
    $sobjectives = Msobjective::all(); 

    $mprogram = Mprogram::find($id); 
    $a = Area::find($mprogram->marea_id); 

    if($a==null){ 
     $area = new Area(); 
     $area->id = $mprogram->marea_id; 
     $area->saveArea(); 
    } 

    $p = Program::find($id); 

    if($p==null){ 
     $program = new Program(); 
     $program->id = $id; 
     $program->initialDate= ""; 
     $program->finalDate= ""; 
     $program->frequency= ""; 
     $program->area_id = $mprogram->marea_id; 
     $program->saveProgram(); 
    } 

}

In initialDate y finalDate de Programm, ich habe eine leere Zeichenfolge, weil, bevor es eine Zeichenfolge war. Ich möchte dort ein Datum angeben, das nicht das tatsächliche Datum sein muss.

+0

Werfen Sie einen Blick zeigen Sie bitte Ihre Code schnelle Lösung zu bekommen –

+0

ich es gerade getan hatte, Danke fürs Helfen! –

+0

@KaliaMartinReina Willkommen bei StackOverflow! Anstatt "gelöst" zum Fragetitel hinzuzufügen, sollten Sie eine der folgenden Antworten akzeptieren: * löst Ihr Problem *. Bitte lesen Sie dies: [Wie funktioniert die Annahme einer Antwort?] (Https://meta.stackexchange.com/a/5235/255183) – ekad

Antwort

0

Verwenden date() Funktion wie:

$date = date('Y-m-d', strtotime('your date')); 

Erläuterung:Y-m-d ist das Standard-Datumsformat für mysql date Typ. Also, was auch immer formatiert Datum Sie haben, konvertieren Sie es in Y-m-d.

+0

Ich würde arbeiten, vielen Dank! Aber jetzt, wo ich weiß, wie das geht, habe ich eine andere Frage. Wenn ich das Standard-Datumsformular ändern möchte, und ich möchte es wie 'd-m-Y' setzen, was müsste ich tun? Ich frage nach der Datenbank. Danke noch einmal. –

+0

Es ist kein guter Ansatz, ändern Sie einfach das Format in Skriptsprache und speichern Sie es in der Datenbank. –

+0

Danke nochmal ..! –

0
<?php 

namespace App\Http\Controllers; 

class YourController extends Controller 
{ 
    public function yourMethod(Request $request) 
    { 
     // If you use a HTML date element, you "should" receive the data in yyyy-mm-dd 
     // format which can be understood by DateTime's constructor 
     $date = new \DateTime($request->input('date')); 

     // If you use a different input or expect a date in a different format you can 
     // use the createFromFormat method, where your first parameter is the date 
     // format you expect and the second is the date input. 
     $date = \DateTime::createFromFormat('d/m/Y', $request->input('date')); 

     // Once your date has been loaded into a DateTime object, you can use the 
     // format method to output the date in any format you like. MySQL and other 
     // databases tend to expect and store dates in yyyy-mm-dd format. 
     echo $date->format('Y-m-d'); 
    } 
} 
+0

Danke, ich verstehe es! –

0

Versuchen Sie es mit Carbon

Carbon::createFromDate($year, $month, $day, $tz); 

oder

Carbon::createFromFormat('Y-m-d H', '1975-05-21 22')->toDateTimeString(); // 1975-05-21 22:00:00 

am documentation