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

Freddie19's avatar

Range time query for email notification

Hello everyone, i'm trying to solve this but keep not solved, so this result will be true if start_time has 30 minutes left before the current time is the same as start_time, but I want the code to be true when it is less than equal to 30 minutes, meaning from 30 until now the time is the same as start_time, it will be executed continuously .

public function getReminder()
{
    $now = Carbon::parse('2023-05-19 14:30:00'); // to test manually. 
	// the actual code : $now = Carbon::now();
    $startTime = $now->copy()->addMinutes(30);

    $bookings = Booking::leftJoin('rooms', 'rooms.id', 'bookings.room_id')
        ->leftJoin('tenants', 'tenants.id', 'bookings.tenant_id')
        ->leftJoin('tenant_details', 'tenant_details.tenant_id', 'tenants.id')
        ->leftJoin('users', 'users.id', 'tenant_details.user_id')
        ->where('bookings.status', 'confirmed')
        ->whereTime('start_time', '<=', $startTime->format("Y-m-d H:i:s"))
        ->whereTime('start_time', '>=', $now->format("Y-m-d H:i:s"))
        ->select(
            ...
        )
        ->get();

    $successfulReminders = [];

    foreach ($bookings as $booking) {
        $users = User::where('email', $booking->email)->get();
        foreach ($users as $user) {
            $user->notify(new BookingReminderNotification($booking));
        }
        if ($users->isNotEmpty()) {
            $successfulReminders[] = $booking->booking_id;
        }
    }

    if (count($successfulReminders) > 0) {
        return response()->json([
				'message' => 'Reminders sent successfully for booking id: ' 
				. implode(', ', $successfulReminders)
		]);
    } else {
        return response()->json(['message' => 'No reminders were sent.']);
    }
}
0 likes
1 reply
LaryAI's avatar
Level 58

To make the code true when it is less than or equal to 30 minutes, the whereTime method should be modified to check if the difference between the start_time and the current time is less than or equal to 30 minutes. This can be achieved by using the diffInMinutes method of the Carbon instance. Here's the modified code:

public function getReminder()
{
    $now = Carbon::now();
    $startTime = $now->copy()->addMinutes(30);

    $bookings = Booking::leftJoin('rooms', 'rooms.id', 'bookings.room_id')
        ->leftJoin('tenants', 'tenants.id', 'bookings.tenant_id')
        ->leftJoin('tenant_details', 'tenant_details.tenant_id', 'tenants.id')
        ->leftJoin('users', 'users.id', 'tenant_details.user_id')
        ->where('bookings.status', 'confirmed')
        ->where('start_time', '<=', $startTime)
        ->where('start_time', '>=', $now->subMinutes(30))
        ->select(
            ...
        )
        ->get();

    $successfulReminders = [];

    foreach ($bookings as $booking) {
        $users = User::where('email', $booking->email)->get();
        foreach ($users as $user) {
            $user->notify(new BookingReminderNotification($booking));
        }
        if ($users->isNotEmpty()) {
            $successfulReminders[] = $booking->booking_id;
        }
    }

    if (count($successfulReminders) > 0) {
        return response()->json([
            'message' => 'Reminders sent successfully for booking id: ' 
                . implode(', ', $successfulReminders)
        ]);
    } else {
        return response()->json(['message' => 'No reminders were sent.']);
    }
}

In the modified code, the whereTime method is replaced with where method to compare the start_time with the $startTime and $now variables. The $now variable is also modified to subtract 30 minutes to get the start time for the range.

Please or to participate in this conversation.