How format date Carbon:now() in Laravel PHP [duplicate]
Asked Answered
F

4

10

I tried

 update(['access' => Carbon::now()->format('Y/m/d H:i:s')]);

it returned Y-m-d H:i:s

Favata answered 6/7, 2018 at 4:38 Comment(9)
I find this very helpful: carbon.nesbot.com/docsSurfbird
What did you want it to return?Nidia
you update this in the database and from the database, you will return only Y-m-d H:i:sMyrticemyrtie
@RobertColumbia DsRaj I want format 'access' is 'Y/m/d H:i:s', and save it in database. but now it save Y-m-d H:i:sFavata
The default format in mysql is Y-m-d H:i:s, so it will save and show in the same format. If you still want to store it as Y/m/d H:i:s, then change data type of date field to varchar.Frog
@LovepreetSingh I have some row 'access' value null. I can't date_format($member['access'], "Y/m/d H:i:s") in file blade Laravel. Error date_format() expects parameter 1 to be DateTimeInterface, string given . How should I do? please help meFavata
Is the date in the database an actual date type, or is it a string type?Eaglestone
What's your question after all? Is there anything not working with the given code, or at least not working up to your requirements?Maricruzmaridel
Reading these comments is like reading 9GAG. It made my day. Thanks, guys!Marital
O
10

now()->format('Y-m-d H:i:s')

You Can Format like above code simply.

Overcharge answered 29/6, 2020 at 8:30 Comment(0)
G
8
<?php
// ...

use Illuminate\Support\Carbon;

//...


$now = Carbon::now()->format('Y-m-d');
Greatniece answered 15/2, 2021 at 7:48 Comment(0)
F
1

The default format in mysql is Y-m-d H:i:s, so it will save and show in the same format. If you still want to store it as Y/m/d H:i:s, then change data type of date field to varchar.

For issue mentioned in comment:

Create date object from string first using date_create and then use date_format to change its format.

$date=date_create($member['access']);
echo date_format($date,"Y/m/d H:i:s");
Frog answered 6/7, 2018 at 5:6 Comment(1)
It OK. Thank you so much!Favata
M
1

You update this in the database and from the database, you will return only Y-m-d H:i:s(Mysql default format) so

While printing the value of access change the format what you needed.

echo date('Y/m/d H:i:s',strtotime($member['access']));
Myrticemyrtie answered 6/7, 2018 at 6:51 Comment(1)
Wow, perfect. date_format(date_create($member['access']), "Y/m/d H:i:s") OK too.Favata

© 2022 - 2024 — McMap. All rights reserved.