Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Sabbir345's avatar

Update dateTime column with Query Builder

if it's possible to update DateTime-columns with Query Builder.

here is my query

$data = Attendance::where('status', '0') ->update(array('status' => '1')) ->update(array('datetime' => format("Y-m-d H:i:s",$datetime)));

0 likes
27 replies
Snapey's avatar

If you add 'updated_at'=>now() in the array of data you are writing to the database

2 likes
Cronix's avatar

Not sure what you mean? Sure you can update them.

DB::table('theTable')->where('id', someId)->update(['datetimeField' => Carbon::now()]);
1 like
Sabbir345's avatar

@Snapey @Cronix

Check my post . its updated

I have date time as a local time but now i am want update with server time

Snapey's avatar

Sorry I don't understand. You want to store the current time, or a specific time?

Local to the timezone of the server?

What timezone is the input?

Cronix's avatar

If $datetime is an actual DateTime object...

$updateData = [
    'status' => 1,
    'datetime' => $datetime->format('Y-m-d H:i:s'),
];

$data = Attendance::where('status', '0')->update($updateData);
1 like
Sabbir345's avatar

Undefined variable: datetime

'datetime' => $datetime->format('Y-m-d H:i:s'),

$datetime this variable is undifined

@Cronix

Cronix's avatar

Then where are you getting $datetime in your original post? I was going off of your original query where you showed you have $datetime... I also said it won't work unless it's a DateTime (php) object.

Where does this value come from?

Its my local time : "02 08 2012 10:09PM"

You're really not helping us to help you by not providing enough info...

We also need to know the name of the timezone that the server is in that you want to convert it to.

Sabbir345's avatar

"02 08 2012 10:09PM" this value come from database

jlrdw's avatar

It's going to be a popcorn night.

Buttered

Cronix's avatar
use Carbon\Carbon;

$date = "02 08 2012 10:09PM";

list($month, $day, $year, $time) = explode(' ', $date);

$updateData = [
    'status' => 1,
    'datetime' => Carbon::parse("$month-$day-$year $time")->toDateTimeString(), // 2012-08-02 22:09:00
];

$data = Attendance::where('status', '0')->update($updateData);

There might be an easier way...

1 like
Sabbir345's avatar

@Cronix Please don't mind i am new in laravel

$date = "02 08 2012 10:09PM"; you define it but its not static . i have several id several date time so how to manage fix datetime problem

Sabbir345's avatar

This date time come from database. Its my local time ...now i want update with server time

Cronix's avatar

Sorry, I don't know what you mean.

The code I did takes this

Its my local time : "02 08 2012 10:09PM"

and converts it to this

now i want update : 2012-02-08 22:09:00

Sabbir345's avatar

@Cronix

$data = Attendance::where('status', '0') ->update(array('status' => '1')) ->update(array('datetime' => format("Y-m-d H:i:s",$datetime)));

Here is my eloquent .... There $datetime is every Person local time attendance

so several user several attendance time. Now i want to update local time to server time

Cronix's avatar

I'm sorry, I think there is a language barrier, or you're not explaining it adequately, or I'm dumb.

Sabbir345's avatar

Actually $datetime its not fixed value . 1st user attendance $datetime is not similar with 2nd user. Its a attendance time

Cronix's avatar

How many rows in the db need to be updated?

jlrdw's avatar

now i want update : 2012-02-08 22:09:00

Take @Cronix code from earlier and just write a function that turns the date time to a stored mysql proper date time format.

It seems you want to store date time properly

yyyy-mm-dd hh:mm:ss

What are they now a datetime or a varchar?

And do you really need time, why not just date? Just a question.

or I'm dumb

Funny

Snapey's avatar

So, am I right, you dont want to change the timezone, you just want to format the value differently ?

diadal's avatar

try this @Sabbir345 you can also format this way d m Y H:i A if 02 is day & if 02 is month m d Y H:i A

$datetime = \Carbon\Carbon::createFromFormat('m d Y H:i A', "02 08 2012 10:09PM");

        $data = Attendance::where('status', '0')->update([

            'status' => '1',
            'datetime' => $datetime

        ]);

Subrang's avatar

$updateData = [ 'status' => 1, 'datetime' => $datetime->format('Y-m-d H:i:s'), ];

$data = Attendance::where('status', '0')->update($updateData);

Please or to participate in this conversation.