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

rishabhgoel9797's avatar

update(Increment) date in database by 1

I have some update query and after checking the where condition I want to increment the date column by 1. How can I do this?

0 likes
7 replies
Cronix's avatar

That depends on what you're doing and how you're doing it. Show your relevant code where you are doing this "update query" and "after checking the where condition." For instance, are you already retrieving the data from the db and just need to update something?

rishabhgoel9797's avatar
public function dayorder_shift(Request $request)
{
    $date_shift = $request->input('day_order');
    //dd($date_shift);

    
    // $data = DB::table('table_academic')->where('date_academic','>',$date_shift)->pluck('date_academic');
    // $data->increment('date_academic',1);

   $data= DB::table('table_academic')->where('date_academic', '>',$date_shift)->increment('date_academic',1);
    return redirect('/calendar')->with('success', "Dates succesfully shifted and updated.");
}
Cronix's avatar

increment() only works on numeric field types, not dates.

You'd have to use raw sql for that. Something like

DB::table('table_academic')
    ->where('date_academic', '>', $date_shift)
    ->update([
        'date_academic' => DB::raw('DATE_ADD("date_academic", INTERVAL 1 DAY)')
    ]);
rishabhgoel9797's avatar

SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect datetime value: 'date_academic'.this error i am getting

Cronix's avatar

what is the column type of date_academic?

Please or to participate in this conversation.