Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Pa6's avatar
Level 1

saving date format

public function hjk() {

    $jj= new HH();   
    $date = date("Y-m-d", time());
    $date = trim($date);
    $time = strtotime($date);
$jj->date = $time;
    $jj->save();
}

not saving the current date, any help? am getting date saved with string not in date format

0 likes
4 replies
bimalshah72's avatar

use Carbon API

 $jj= new HH();      
$jj->date = Carbon\Carbon::now();
 $jj->save();
Pa6's avatar
Level 1

I used that but it is saving the year only, while i want to save date, month and year,

bobbybouwmann's avatar

You only want the date? In that case you need to specify the format for the database

// Laravel will know how to handle this
$jj->dateTime = Carbon::now();

// Specify the format you want to save to the database
$jj->date = Carbon::now()->format('Y-m-d');

Please or to participate in this conversation.