I need to show the passed years like 5.6 years with a given date $model->created_at
What I tried so far is
$dt = Carbon::now();
echo $dt->diffForHumans($model->created_at); 1 month ago
how to show date only like 5.6 years
you can use Carbon::floatDiffInRealYears to get exactly the different,
and Carbon::diffInYears to get the rounded result of the previous method
$dt = Carbon::now();
echo $dt->floatDiffInRealYears($model->created_at);
Related
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);
I need help with CarbonPeriod. I'm creating reminder to take medicine from one date to another in hours interval like this:
$startDate = Carbon::parse($this->data->get('date'))->setTimezone($timezone);
$endDate = Carbon::parse($this->data->get('endDate'))->addDay()->setTimezone($timezone);
$interval = $this->data->get('interval');
$period = new DatePeriod($startDate, CarbonInterval::hours($interval), $endDate);
but now I need to create CarbonPeriod knowing only $startDate and $interval in days and not the $endDate. Like I want to take my tablet every day in 12 o'clock until I turn off this reminder myself. Can somebody help me with this?
Typically when doing this, I would simply set the end date a very long way in the future (10 years or 20 years or 30 years ... you get the idea) and also (if you think it will ever get close to that date) have a mechanism when checking the date to extend it when it gets close (i.e. a rolling end date as such).
You can do the following:
$date = Carbon::now();
$period = $date->toPeriod(null, 3, 'days');
$period->setRecurrences(INF);
This will create a never-ending period that jumps 3 days at a time.
Examples directly from the documentation:
CarbonPeriod::create('now', '2 days', INF); // infinite iteration
CarbonPeriod::create('now', '2 days', INF)->calculateEnd()->isEndOfTime(); // true
CarbonPeriod::create('now', CarbonInterval::days(-2), INF)->calculateEnd()->isStartOfTime(); // true
https://carbon.nesbot.com/docs/#api-period
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');
In order to list all the articles (films) that are included in our catalog the current month I have written the following query using the Laravel:
$current_month_film = DB::table('films')
->join('categories', 'films.category_id', '=', 'categories.id')
->select('films.*', 'categories.*')
->whereMonth('films.created_at', '=', '12')
->orderBy('films.created_at', 'desc')
->get();
It works perfect. It shows 5 films were added this month. The only problem is that I am hard coding December (month = 12). Next month I will need to update to month = 1. And so on each month; that is a bad solution.
The questions are:
1- How could I get always the current month? Trying
->whereMonth('films.created_at', '=', 'NOW')
give back not error, but the list comes empty. In December there are 5 films added.
2- I am showing a message in front end:
5 films added in Month 12
Is there a way to change the month = 12 into December and show a friendlier message like:
5 films added in December"
3- A better approach would be to show films included within the last 30 days. I did not find a time function for that.
Try to utilise Carbon which comes by default with Laravel.
->whereMonth('films.created_at', '=', Carbon::now()->month)
Since you know $month = 12, you can do something like
$someDate = Carbon::parse("2016" . $month . "01"); //someDate is a Carbon object now
$output = "added in Month " . $someDate->format('F');
Retrieve data 1 month old:
->whereBetween('films.created_at', [Carbon::today()->subMonth(), Carbon::today()])`
Edit: Put in use Carbon; before your class SomeClassName {. You might want to read up about Namespace
I want to set 10 years back date from today date in kendo datepicker.
If you want to initialize your date picker with 10 years back date always, then in your HTML it would be something like this:
#(Html.Kendo().DatePicker().Name("myDatePicker").Value(DateTime.Today.AddYears(-10)))
For an example: When we are working with any Bank form applications, the applicant should have minimum 20 years from Today's Date to apply a particular Exam.
So It's Better to give Max-Date as the Day 20 years from Today's Date
It is very easy to set past 'n' number of years date from today date in kendo datepicker.
Find the small snippet below.
var toDate = new Date();
// Here 'n' Excludes number of years from today's Date.
var pastDate= new Date(toDate.setFullYear(toDate.getFullYear()- n));
$("#birthDayDate").kendoDatePicker({
max: pastDate
});