If you add 'updated_at'=>now() in the array of data you are writing to the database
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)));
Not sure what you mean? Sure you can update them.
DB::table('theTable')->where('id', someId)->update(['datetimeField' => Carbon::now()]);
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?
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);
Undefined variable: datetime
'datetime' => $datetime->format('Y-m-d H:i:s'),
$datetime this variable is undifined
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.
"02 08 2012 10:09PM" this value come from database
It's going to be a popcorn night.
Buttered
is 02 the day, or the month?
month
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...
@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
This date time come from database. Its my local time ...now i want update with server time
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
$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
I'm sorry, I think there is a language barrier, or you're not explaining it adequately, or I'm dumb.
@Cronix i am sorry. You are really helpful person
Actually $datetime its not fixed value . 1st user attendance $datetime is not similar with 2nd user. Its a attendance time
How many rows in the db need to be updated?
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
So, am I right, you dont want to change the timezone, you just want to format the value differently ?
Here is a decent post on storing user time zones. https://laracasts.com/discuss/channels/general-discussion/user-specified-timezones-config
You could just do a one time loop through the database records and update as needed, using or passing the currently stored date time field to the custom function, change as needed, and update that record.
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
]);
$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.