Laravel carbon and subDays - laravel

I am trying to get list of users with created_at date more than 30 days but it get users with even today date.
Logic
Get users which registration dates +30 days.
If their verification date is null delete them.
Issue
even if I register today and my verification date is null my account will delete.
Code
$date = Carbon::today()->subDays(30);
$deleteUsers = User::where('email_verified_at', null)->where('created_at', '>', $date)->delete();

$date=Carbon::today()->subDays(30);
$deleteUsers = User::where('email_verified_at', null)->where('created_at', '<', $date)->delete();

We compare this date code with the previous one if it was smaller
code:
$date=Carbon::now()->subDays(30)->format('Y-m-d');
$t=Trash_form::where('updated_at','<=',$date)->value('id');
return Trash_form::where('id',$t)->delete();

Related

Laravel, how to delete all records but the first one of that day

So I have a table to log profile followers every 10 minutes to keep a record of the increase/decrease.
However after the current day I only need to keep the last record of that day. Is there a simple way in Laravel to delete all records a part from the last one recorded every day.
I've tried searching and searching but comes up with nothing and feel like I'm going to create something overly complicated to accomplish this.
You'd need a query like this.
DELETE
FROM logs
WHERE id IN (
SELECT id
FROM logs
WHERE created_at BETWEEN 2022-02-28 00:00:00 AND 2022-02-28 23:59:59
ORDER BY created_at DESC
OFFSET 1
)
Assuming you have a date variable, you could make the query like this using the whereDate method:
$date = '2022-02-28'; // Y-m-d format. This is important.
DB::table('logs')
->whereIn('id', function ($sub) use ($date) {
$sub->select('id')
->from('logs')
->whereDate('created_at', $date)
->orderByDesc('created_at')
->offset(1);
})
->delete();

How to check if voucher is expired after it purchased one year.?

I am building a voucher generate system in laravel. This voucher validity period is one year. Now i want do is when user trying to redeem a voucher then user cannot do it because of it is expired. Any one can help me to do this. Because I don't have any idea about this.
This is what I am tried
$voucher = Voucher::where('code', $request->code)->first();
$today = date("Y-m-d H:i:s");
$startdate = $voucher['created_at'];
$offset = strtotime("+365 day");
$enddate = date($startdate, $offset);
$today_date = new \DateTime($today);
$expiry_date = new \DateTime($enddate);
if ($expiry_date < $today_date) {
return response()->json(['error' =>"Voucher is expired"]);
}
Assuming you're storing the vouchers in the database, you can add a valid_until date column and verify that it's in the past before redeeming the voucher.
Basing the expiry date off the created_at field could be problematic in the future if the expiry rules change. For example consider what would happen if after a while you decide that vouchers should actually expire after 6 months. If I had purchased a voucher which was expected to last a year then it wouldn't be fair that mine expires early.
A better approach as mentioned in a previous answer would be to store the expiry date in a separate column in the table. This would make the code that checks for expiry much simpler and also would allow you to have different expiry times if required, e.g. a premium voucher could last for 2 years instead of just one.

Laravel/PHP/Carbon parse a date to one and three days from now() to the hour, disregarding minutes and seconds

I'm using Laravel 5.4 and Carbon to find dates that are exactly one day and three days prior to a laravel job (to the hour) to send notifications. So for example if the date was 2019-03-10 18:00:03 or 2019-03-10 18:15:34, I'd like to send a notification tomorrow at 2019-03-07 18:00:00 and another at 2019-03-09 18:00:00. I'd like to do this in an eloquent query, but not really sure how to "lazy match" by day and hour. If I have an Appointment model with a start_date timestamp, I know I can do exact matches by day or date, but I want to find matches that disregard the hours/seconds. I was considering something along these lines:
$oneDayAppointments = Appointment::whereDay(
'start_date',
Carbon::now()->addDay()->day
)->get();
...would get me any appointments where the start_date is a day from now. Now how would I hone it to the day and hour. If I were to just use a where clause:
$oneDayAppointments = Appointment::where(
'start_date',
Carbon::now()->addHours(24)
)->get();
...I could get exact matches, but only if the minutes/seconds were a match as well. Thanks!
Working on my comment above, this should do it
$tomorrowStart = Carbon::createFromFormat('Y-m-d H:i:s', Carbon::now()->addDay()->format('Y-m-d H:00:00'));
$tomorrowEnd = $tomorrowStart->copy()->addHour()->subSecond();
//whereBetween('start_date', [$tomorrowStart, $tomorrowEnd])
https://laravel.com/docs/5.8/queries

Check today is between start date and expired date using Carbon?

I have to check whether coupon is active, or no by checking that today is between start date and expired date.
My start date and expired date format is 01/12/2017
Let assume, today is 21/06/2017
Coupon start date is 19/06/2017 and expired date is 23/06/2017
So it should result in Coupon is Active, wheter today is less than coupon start and expired date it should result in Coupon is Expired
So far I didn't find any tutorial which comparing today with start and expired date using Carbon.
Almost all tutorial like this
$now = Carbon::now();
$end_date = Carbon::parse($request->input('end_date'));
$lengthOfAd = $end_date->diffInDays($now);
So How I can check if today is between start date and expired date, and today is less than start date and expired date using Carbon in Laravel?
Thanks in advance.
It's a very simple task using Carbon, all you can do is call the between method of carbon :
$now = Carbon::now();
$start_date = Carbon::parse($request->input('start_date'));
$end_date = Carbon::parse($request->input('end_date'));
if($now->between($start_date,$end_date)){
echo 'Coupon is Active';
} else {
echo 'Coupon is Expired';
}
You can add a third parameter to the between method to use or equal like discribed in the doc
To determine if the current instance is between two other instances you can use the aptly named between() method. The third parameter indicates if an equal to comparison should be done. The default is true which determines if its between or equal to the boundaries.

Laravel Eloquent Birthdays Query to get Date Column based on Date and Month only

I have a date() column in mysql which has the Date of Birth in mysql’s YYYY-MM-DD format:
$table->date('dob')->nullable();
I am trying to query this table so it lists all users who are having birthdays from today till next 7 days. I have something like this right now:
$date = new Carbon;
$today = $date->format('Y-m-d');
$futureDays = $date->addDays(7)->format('Y-m-d');
$birthdays = SiteUsers::where('dob', '>=', $today)
->where('dob', '<=', $futureDays)
->orderBy('dob', 'ASC')
->get();
The problem with the above query is that it only lists the users having DOB with the exact dates in this year only which is not right. What I really want is to list the users having birthdays irrespective of the year they were born by matching only the ‘date’ and ‘month’ and ignoring the ‘year’.
I don’t know how to achieve that. Can someone help me out please…
Shortly after I posted my question, I tried a method using raw queries that uses DateOfYear() SQL Function. So this is what I have now which seems to be working by getting the users with birthdays in the next 7 days irrespective of the year:
$birthdays = SiteUsers::whereRaw('DAYOFYEAR(curdate()) <= DAYOFYEAR(dob) AND DAYOFYEAR(curdate()) + 7 >= dayofyear(dob)')
->orderByRaw('DAYOFYEAR(dob)')
->get();
enjoy

Resources