Laravel how to get current Time and Date value

26,816

Solution 1

try this

public function store(Request $request)
    {
      $request['time']=date("Y-m-d h:i:s a", time());
        attendance::create($request->all());
                return view('traineeattendance.attendanceo');
    }

Solution 2

Laravel has the Carbon dependency attached to it.

Carbon::now(), include the Carbon\Carbon namespace if necessary.

Edit (usage and docs)

Say I want to retrieve the date and time and output it as a string.

$mytime = Carbon\Carbon::now();
echo $mytime->toDateTimeString();

This will output in the usual format of Y-m-d H:i:s, there are many pre-created formats and you will unlikely need to mess with PHP date time strings again with Carbon.

Documentation: https://github.com/briannesbitt/Carbon

String formats for Carbon: http://carbon.nesbot.com/docs/#api-formatting

Solution 3

You might want to use Carbon function here

Refer some docs: https://github.com/briannesbitt/Carbon

Share:
26,816
Dasun
Author by

Dasun

Updated on July 09, 2022

Comments

  • Dasun
    Dasun almost 2 years

    I`m developing an Attendance Management system.so that my supervisor ask me to develop following view to get attendance time and date value.

    enter image description here Few requirements i need.

    1. All the records need to be saved in the database based on Trainee Id.according to that view if one having above ID check the box and then press the mark button that time value and Trainee id must be saved in the database as on time.

    2. Then if once mark it Date value also need to be saved in the database.

    3. Then again check it and press the mark button it must go their as Off time.only 2 records need to be allowed otherwise their can be many on off values.

    can anyone suggest me the relevant controller function for that?

    here is the relevant above view

    <table class="table table-striped">
          <thead>
    
           <th>Trainee ID</th>
            <th>Name with Initials</th> 
            <th>Time</th>
            <th>Mark Here!</th>
          </thead>
    
          <tbody>
            @foreach($items as $item)
    
        <tr>
    
         <td>{{ $item->trainee_id }}</td>
          <td>{{ $item->name_with_initials }}</td>
    
    
            <td>
            <label><input type="checkbox" value="">&nbsp; Time</label>
          </td>
    
            <td>
            <a class="btn btn-info" href="Bankdetails/{{ $item->trainee_id }}">Mark Here</a>
          </td>
      </tr>
            @endforeach
    
          </tbody>
    
    </table>