skip to Main Content

So, currently, my web-app is working correctly in local. The problem in shared hosting is that when I clock out, it automatically changes all the time clock-out and location clock-out of all the other users as well. Here is the screenshot in local, https://paste.pics/48b6310c2e1bdf728ccedc9ed6fd42ce and here is the screenshot when deployed to shared hosting(cPanel), it is behaving like this, https://paste.pics/f0a9c1aafc62e5edbb0157bf8c1de1e6 .

I’ve tried reuploading everything for third times, double checking on the controller file in shared hosting, api.php in routes, and the blade.php file in views, resetting all the data and restarting again and it’s still the same in shared hosting. I wonder what’s wrong ? Please help, I’ve stucked at this bug since last week friday.

Below are the code taken from hosting using WinSCP

Api.php in routes

<?php

use IlluminateHttpRequest;
use IlluminateSupportFacadesRoute;
use AppHttpControllersAuthController;
use AppHttpControllersUserProfileController;


Route::post('login', [AuthController::class, 'login']);

Route::get('getdata', [UserProfileController::class, 'getdata']);
Route::post('getdata/{id}', [UserProfileController::class, 'showdata']);
Route::post('adduser', [UserProfileController::class, 'adddata']);
Route::delete('deleteuser', [UserProfileController::class, 'deleteuser']);
Route::PUT('updateuser', [UserProfileController::class, 'updateuser']);
Route::post('updateuserClockIn',   [UserProfileController::class, 'userClockIn']);
Route::post('updateuserClockOut', [UserProfileController::class, 'userClockOut']);

Route::middleware('auth:sanctum')->get('user', function (Request $request) {
    return $request->user();
});

function in UserProfileController,

public function userClockIn(Request $r)

    {

        $result = [];
        $result['status'] = false;
        $result['message'] = "something error";


        $users = User::where('staff_id', $r->staff_id)->select(['staff_id', 'date_checkIn', 'time_checkIn', 'location_checkIn'])->first();


        $mytime = Carbon::now('Asia/Kuala_Lumpur');
        $date = $mytime->format('Y-m-d');
        $time = $mytime->format('H:i:s');

        $users->date_checkIn = $date;
        $users->time_checkIn = $time;
        $users->location_checkIn = $r->location_checkIn;

        AttendanceRecord::updateOrCreate(
            ['staff_id' => $users->staff_id, 'date_checkIn' => $date],
            $users->toArray()
        );

        $result['data'] = $users;
        $result['status'] = true;
        $result['message'] = "suksess add data";

        return response()->json($result);
    }

public function userClockOut(Request $r)

    {

        $result = [];
        $result['status'] = false;
        $result['message'] = "something error";

        $users = User::where('staff_id', $r->staff_id)->select(['staff_id', 'time_checkOut', 'location_checkOut'])->first();

        $mytime = Carbon::now('Asia/Kuala_Lumpur');
        $date = $mytime->format('Y-m-d');
        $time = $mytime->format('H:i:s');

        $users->date_checkIn = $date;
        $users->time_checkOut = $time;
        $users->location_checkOut = $r->location_checkOut;

        // Save the updated data to the database

        AttendanceRecord::updateOrCreate(
            ['staff_id' => $users->staff_id, 'date_checkIn' => $date],
            $users->toArray()
        );

        $result['data'] = $users;
        $result['status'] = true;
        $result['message'] = "suksess add data";

        return response()->json($result);

}

blade.php files in views displaying the History page,

<tbody class="bg-white divide-y divide-gray-200">
    @php
    $no = 1
    @endphp
    @foreach ($history as $row)
    <tr>
      <td class="px-6 py-4 whitespace-nowrap">
        <div class="text-sm text-gray-900"> {{ $no++ }} </div> 
      </td>

      <td class="px-6 py-4 whitespace-nowrap">
        <div class="text-sm text-gray-900"> {{ $row->staff_id }} </div>
      </td>

      <td class="px-6 py-4 whitespace-nowrap"> 
        <div class="text-sm text-gray-900"> {{ $row->date_checkIn }} </div> 
      </td>

      <td class="px-6 py-4 whitespace-nowrap"> 
        <div class="text-sm text-gray-900"> {{ $row->time_checkIn }} </div>
      </td>
      
      <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
        {{ $row->location_checkIn }}
      </td>
                        
      <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500">
        {{ $row->time_checkOut }}
      </td>
                        
      <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-500"> 
        {{ $row->location_checkOut }} 
      </td>
      </tr>
      @endforeach
</tbody>

2

Answers


  1. I guess it’s related to the database server timezone.
    You can check the database timezone of your local and remote databases and set them (UTC, for exemple).

    1. Connect to both databases and check theirs values:

    SELECT @@global.time_zone, @@session.time_zone, @@system_time_zone, CURRENT_TIMESTAMP;

    You must pay attention to the current_timestamp or @@session.time_zone.

    So, you can set them to the expected timezone using the expected timezone on the model.

    On the PHP side, you can set the default timezone modifying the connection options (taking in account the databases values):

    'mysql' => [
        'driver'    => 'mysql',
        'host'      => env('DB_HOST', 'localhost'),
        'database'  => env('DB_DATABASE', 'forge'),
        'username'  => env('DB_USERNAME', 'forge'),
        'password'  => env('DB_PASSWORD', ''),
        'charset'   => 'utf8',
        'collation' => 'utf8_unicode_ci',
        'prefix'    => '',
        'strict'    => false,
        'timezone'  => '+00:00' // here
    ],
    
    Login or Signup to reply.
  2. // Maybe?
    if ("UTC" != date_default_timezone_get()) {
        date_default_timezone_set('UTC');
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search