Trupti
Trupti

Reputation: 973

How to save date in unix timestamp in laravel?

I have one column as closed_date and data type is timestamp. I want to add the date in unix timestamp e.g. 1556801668. I don't know how to do that in Laravel.

Upvotes: 9

Views: 9558

Answers (3)

user5446912
user5446912

Reputation:

I you want to do this within the model, you can use castings:

protected $casts = [
    'closed_date' => 'timestamp',
];

Upvotes: 3

Leonardo Rossi
Leonardo Rossi

Reputation: 3022

If you have a Date string, for instance, you can create a Carbon object and store it directly into database.

Laravel will convert that object to the correct type for the database.

Upvotes: 0

Tharaka Dilshan
Tharaka Dilshan

Reputation: 4499

use php strtotime() function to convert time.

Example

$time = '2019-01-01'

$timeStamp = strtotime($time);

// will output 1546300800

Upvotes: 7

Related Questions