andyiscoding
andyiscoding

Reputation: 43

Laravel 7 change date format to timestamp

Hello so right now I have this data that i retrieved from the database table and the format is ["2021-08-17 23:13:15"] and i wish to change it into something like this 1629243333 (another data) because i want to get the difference in days between both time. I tried using the gettimeStamp() but it doesnt work.

$updateRaw = \DB::table('tickets')
        ->where('id', $ticket)
        ->pluck('updated_at');

the $updateRaw is the data with the date format. Any help would be appreciated thanks

Upvotes: 1

Views: 854

Answers (2)

N69S
N69S

Reputation: 17205

A simpler solution would be to use native strtotime() to convert datetime to unix timestamp

$updateRaw = \DB::table('tickets')
        ->where('id', $ticket)
        ->value('updated_at'); //use value to get first value directly

$timestamp = strtotime($updateRaw);

Upvotes: 0

Pradeep
Pradeep

Reputation: 9717

With Carbon do like this, if update_at column is not carbon instance;

use Carbon\Carbon;
Carbon::parse($updateRaw[0])->timestamp;

if it is then simply do

$updateRaw[0]->timestamp;

for more : https://carbon.nesbot.com/

Upvotes: 1

Related Questions