May Sale! All accounts are 40% off this week.

boldstar's avatar

How To Create Timestamp from Date

Trying to create a time stamp from date. not sure what I am doing wrong. When I send my date it is in the following format

04/02/2019

I would like to change it to a timestamp like so

2019-04-02 15:25:37

When I try

 Carbon::createFromFormat('Y-m-d H:m:s', $request->date)->timestamp;

I get the following alarm

"Trailing data"

If i try

Carbon::parse($request->date)->timestamp;

I get the following alarm

Creating default object from empty value"

and lastly i tried the php method strtotime($request->date) which also gave me the same alarm.

Any ideas?

0 likes
9 replies
boldstar's avatar

@JLRDW - thanks for the reply, I am trying to create a time stamp from the date I am sending. Basically I want to over write the created_at date in my database with the new date.

database time stamp looks like

2019-03-19 21:01:00
jlrdw's avatar

Have you tried.

$mydate = now();
lindstrom's avatar

Assuming you want to use the date you pass and don't want to simply use now(), you can:

YourModel::where('id, $someId)->update([
    'created_at' => Carbon::parse($request->date),
]);

The created_at field is already cast to Carbon so you can save the Carbon instance directly--no formatting required.

homeoffice's avatar

A timestamp is a succession of characters or encoded data recognizing when a specific occasion happened, normally giving date and time of day, some of the time precise to a little division of a second. I have the same issue for making a current timestamp for https://professionalhomeandofficeservices.com.au/aboutus website. can you share the proper method?

boldstar's avatar

@lindstrom , the correct method was Carbon::parse(), the problem was I was passing in the wrong data. Thanks for the help!

Here is the code snippet

\Carbon\Carbon::parse($request->date)->timestamp;
lindstrom's avatar

@BOLDSTAR - Glad you figured it out. But, it still doesn't answer the question of why you are converting it to a timestamp because if you are using created_at, it's going to get cast to Carbon when you save it. Try it in Tinker (php artisan tinker) if you aren't convinced:

// From Tinker

>>> $user = User::find(1);
=> App\Models\User {#3602
     id: 1,
     first_name: "Shawn",
     last_name: "Lindstrom",
     email: "[email protected]",
     username: "slindstrom",
     created_at: "2019-04-02 03:28:51",
     updated_at: "2019-04-02 03:28:51",
   }
>>> $user->created_at = \Carbon\Carbon::now()->timestamp;
// Here we see that created_at is set to the current timestamp
=> 1554316597
>>> $user->save();
=> true
// But, after we save the model and retrieve created_at we see that it's a Carbon instance because it was cast to Carbon when we saved it
>>> $user->created_at
=> Illuminate\Support\Carbon @1554175732 {#3570
     date: 2019-04-02 03:28:52.0 UTC (+00:00),
   }
// Now, if we actually want a timestamp we have to call the timestamp method
>>> $user->created_at->timestamp
=> 1554316597
boldstar's avatar

@LINDSTROM - well when I try to update my created_at date without using Carbon::parse() it throws an error about trailing data. I am not sure what trailing data is or what about it that carbon does not like.

Ive tried sending the date to be saved as 2 different formats without using carbon

1st

Mon Mar 18 2019 00:00:00 GMT-0500 (Central Daylight Time)

2nd

03/18/2019

my controller without carbon(Doesnt Work)

try {
    $action->created_at = $request->date;
    $action->save();
 } catch(\Exception $e) {
     return response(['message' => $e->getMessage()], 422);
 }    

with carbon(Works)

try {
    $action->created_at = \Carbon\Carbon::parse($request->date)->timestamp;
    $action->save();
 } catch(\Exception $e) {
      return response(['message' => $e->getMessage()], 422);
  }
Evanz's avatar

You can try

date('Y-m-d H:i:s', strtotime($request->date));

Please or to participate in this conversation.