14

I want to store the current date time in MySQL using the following Laravel function. Actually, I stored a static date. Instead of this, how can I store the current date time in the created_at and updated_at fields in the database?

function insert(Request $req)
{
    $name = $req->input('name');
    $address = $req->input('address');
    $data = array("name" => $name, "address" => $address, "created_at" => '2017-04-27 10:29:59', "updated_at" => '2017-04-27 10:29:59');
    DB::table('student')->insert($data);
    echo "Record inserted successfully.<br/>";
    return redirect('/');
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
seema
  • 344
  • 1
  • 3
  • 11
  • Solution is already available @ https://stackoverflow.com/questions/28109179/getting-current-date-time-day-in-laravel – Vivek Dec 13 '18 at 04:47
  • yes i checked but error occur at Carbon\Carbon namespace so that i asked question .. – seema Dec 13 '18 at 04:50

5 Answers5

26

Use the Laravel helper function

now()

Otherwise, use the carbon class:

Carbon\Carbon::now()

It is used like this:

$data = array("name" => $name,"address" => $address,"created_at"=> Carbon::now(),"updated_at"=> now());
DB::table('student')->insert($data);

For more information, see now()

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jignesh Joisar
  • 13,720
  • 5
  • 57
  • 57
3

You can also use this to get the current date time. It's working.

$current_date = date('Y-m-d H:i:s');

And also I have updated and set things in your function. You have to add this:

function insert(Request $req)
{
    $name = $req->input('name');
    $address = $req->input('address');

    $current_date_time = date('Y-m-d H:i:s');

    $data = array("name" => $name,
                  "address" => $address,
                  "created_at" => $current_date_time,
                  "updated_at" => $current_date_time);
    DB::table('student')->insert($data);
    echo "Record inserted successfully.<br/>";
    return redirect('/');
 }
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dhara gosai
  • 92
  • 10
  • Notice that this solution will use the PHP server date-time, not necessarily the same date-time as the database server you would get using now() in MySQL or getDate() in SQLServer. – Don G. Aug 24 '22 at 17:32
3

Use this in your database query:

'created_at' => Carbon::now(),
'updated_at' => Carbon::now()
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Prathamesh Doke
  • 797
  • 2
  • 16
  • 38
1

You can use the DateTime object.

Look at the below code:

$curTime = new \DateTime();
$created_at = $curTime->format("Y-m-d H:i:s");

$updateTime = new \DateTime();
$updated_at = $updateTime->format("Y-m-d H:i:s");
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ravi Sachaniya
  • 1,641
  • 18
  • 20
-1

Use

use Carbon\Carbon;

at the header of the controller. This code work for Laravel 9:

$updated = Carbon::now();

You may save it inside a variable and use the variable for inserting and updating statements or for any other purpose.

During migration, I defined the column check_at as datetime. And I used the following command for table creation.

php artisan migrate

It generates a timestamp-type column in MySQL. I am passing the variable $updated for update purposes. While using phpMyAdmin, I can see the column check_at has data in date-time format.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rajib
  • 392
  • 4
  • 16