Check today is between start date and expired date using Carbon? - laravel-5

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.

Related

Carbon: comparing date instances is giving unexpected result

This problem is driving me crazy.
Here is my Carbon instance:
$carbonTemp = CarbonImmutable::createFromFormat('Y-m-d', $date);
What I want to do is just to understand if the date here is the last day of the week.
$carbonTemp->equalTo($carbonTemp->endOfWeek())
If $date = '2021-08-08' which is the last day of this week, the result above must be true, right?
It is giving me FALSE !!
Even though $carbonTemp->endOfWeek()->format('Y-m-d') is giving me '2021-08-08'.
endOfWeek() returns last day of week and time 23:59:59.999999. Therefore it is necessary to use endOfDay() method when creating date.
$carbonTemp = CarbonImmutable::createFromFormat('Y-m-d', $date)->endOfDay();
that's because you created CarbonImmutable object, so endOfWeek will not work.
Create a normal Carbon date:
$carbonTemp = Carbon::createFromFormat('Y-m-d', $date);

Can't get monthy CarbonPeriod to behave as desired

In the project I'm working on I have a daily command that basically checks the date of the last record in the database and tries to fetch data from an API from the day after and then each month after that (the data is published monthly).
Basically, the last record's date is 2019-08-30. I'm mocking as if I were running the task on 2019-09-01 with
$test = Carbon::create(2019,9,1,4);
Carbon::setTestNow($test);
I then create a monthly period between the next day of the last record's date and the last day of the current month like so:
$period = CarbonPeriod::create($last_record_date->addDay(), '1 month', $last_day_of_current_month);
Successfully generating a period with start_date = 2019-08-31 and end_date = 2019-09-30. Which I use in a simple foreach.
What I expected to happen is that it runs twice, once for August and once for September, but it's running only once for the start date. It's probably adding a month and going past the end date, but I don't know how to force the behaviour I'm looking for.
TL;DR:
$period = CarbonPeriod::create('2019-08-31', '1 month', '2019-09-30');
foreach ($period as $dt) {
echo $dt->format("Y-m") . "<br>\n";
}
This will print just 2019-08, while I expect 2019-08 and 2019-09. What's the best way to achieve that?
Solution :-
You can store actual date in $actual_day and current date for occurring monthly in $current_day. Put a check on comparing both dates, if not matched then make it on the same day it will skip 30,31 case in case of February month.
$current_date = $current_date->addMonths(1);
if($current_date->day != $actual_day){
$date = Carbon::parse($date->year."-".$date->month."-".$actual_day);
}
Your start date is 2019-08-31. Adding a month takes you to 2019-09-31. 2019-09-31 doesn't exist so instead you get 2019-10-01, which is after your end date. To avoid this I'd suggest you use a more regular interval such as 30 days.
Otherwise you're going to have to rigorously define what you mean by "a month later". If the start date is 31st Jan is the next date 28th February? Is the month after 28th or 31st March? How do leap years affect things?

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.

Get First day of last month timestamp with Carbon

I want to get the timestamp in milliseconds for first day of last month and last day of last month using Carbon in laravel.
I have tried to do it with carbon::parse, which is achieved. But I want to instantiate the Carbon class simply to achieve the same.
This is the code that works fine with Carbon::parse
Carbon::parse('first day of last month',$timezone)->timestamp
But I want to achieve the same using something like below.
$start = new Carbon('first day of last month');
$end = new Carbon('last day of last month');
The output should be timestamp in milliseconds. Like 1555704794000
You may try the below code
$previous_month_start = Carbon::now()->subMonth()->startOfMonth()->format('x');
$previous_month_end = Carbon::now()->subMonth()->endOfMonth()->format('x');

Laravel carbon and subDays

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();

Resources