9

I tried

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

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

Derrick
  • 3,669
  • 5
  • 35
  • 50
ThaiVD
  • 117
  • 1
  • 1
  • 3
  • 2
    I find this very helpful: https://carbon.nesbot.com/docs/ – hungrykoala Jul 06 '18 at 04:40
  • 1
    What did you want it to return? – Robert Columbia Jul 06 '18 at 04:46
  • you update this in the database and from the database, you will return only Y-m-d H:i:s – DsRaj Jul 06 '18 at 04:46
  • @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:s – ThaiVD Jul 06 '18 at 04:52
  • 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. – Lovepreet Singh Jul 06 '18 at 04:55
  • @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 me – ThaiVD Jul 06 '18 at 05:02
  • Is the date in the database an actual date type, or is it a string type? – munsifali Jul 06 '18 at 05:04
  • What's your question after all? Is there anything not working with the given code, or at least not working up to your requirements? – Nico Haase Jun 28 '19 at 08:12
  • Reading these comments is like reading 9GAG. It made my day. Thanks, guys! – Jhourlad Estrella Nov 17 '19 at 05:37

4 Answers4

9

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

You Can Format like above code simply.

JOY KUMAR PAL
  • 136
  • 1
  • 5
8
<?php
// ...

use Illuminate\Support\Carbon;

//...


$now = Carbon::now()->format('Y-m-d');
Pathros
  • 10,042
  • 20
  • 90
  • 156
kush
  • 595
  • 5
  • 7
0

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");
Lovepreet Singh
  • 4,792
  • 1
  • 18
  • 36
0

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']));
DsRaj
  • 2,288
  • 1
  • 16
  • 26