1

I have a string 2020-09-18T07:58:03-04:00 which i want to convert into timestamp format before storing to database. anyone can help me with this?

4
  • What have you tried? What errors are you getting? We're happy to help, but you need to make an attempt to solve the issue yourself :) Commented Sep 22, 2020 at 16:43
  • use this date('d -m -Y',strtotime('2020-09-18T07:58:03-04:00')) .. Commented Sep 22, 2020 at 16:46
  • 1
    or use this date('Y -m -d',strtotime('2020-09-18T07:58:03-04:00')) Commented Sep 22, 2020 at 16:47
  • Hi, welcome to SO. Please update your question and provide a MRE (stackoverflow.com/help/minimal-reproducible-example) with relevant code parts. Commented Sep 22, 2020 at 18:08

2 Answers 2

2

You can do

Carbon::parse('2020-09-18T07:58:03-04:00')->toDateTimeString();

// OR

Carbon::parse('2020-09-18T07:58:03-04:00')->format('Y-m-d H:i:s');

Result: 2020-09-18 07:58:03

Sign up to request clarification or add additional context in comments.

2 Comments

Actually i want to get a timestamp like this 2020-09-22 14:59:11
2020-09-22 14:59:11 This is not timestamp check the update answer
2

This should do the trick if you want a timestamp:

Carbon::parse('2020-09-18T07:58:03-04:00')->timestamp;

But I don't think that you need a timestamp to store the date. Parsing should be enough.

$model->my_date_field = Carbon::parse('2020-09-18T07:58:03-04:00');
$model->save();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.