How to get datetime differance in laravel 4 - laravel

I am using laravel 4. But I am facing problem with finding the difference between two date: one coming from database table and another one is current datetime. From the date difference I am expecting 1 hour or 1 day. I've tried few solution but can't fix this yet. And also I don't know the better way to solve it. If you guys have any solution, please provide me an example. Please tell me if I need any library. Here is my code:
$lecture_id = Input::get('lecture_id');
$delegate_id = Input::get('delegate_id');
// $newDate = new Datetime();
$lecture = Lecture::find($lecture_id);
// $lec_date = Date::forge($lecture->start_time);
// $lec_date = new Datetime($lecture->start_time);
$lec_date = $lecture->start_time->diffForHumans(Carbon::now());
if ( $lec_date > 1) {
LectureDelegate::create(array(
'lecture_id' => Input::get('lecture_id'),
'delegate_id'=> Input::get('delegate_id')
));
return Redirect::to('/')->with('message', 'Your are successfully apply to the lecture');
}

Should be:
$lec_date = Carbon::createFromTimeStamp( strtotime( $lecture->start_time ) )->diffForHumans();
or possibly:
$lec_date = $lecture->start_time->diffForHumans();
If you add this to your Lecture.php model:
public function getDates()
{
return array('created_at', 'updated_at', 'deleted_at', 'start_time');
}
From the documentation:
By default, Eloquent will convert the created_at, updated_at, and
deleted_at columns to instances of Carbon...
You may customize which fields are automatically mutated, and even
completely disable this mutation, by overriding the getDates method of
the model.
As for diffForHumans the documentation states:
The lone argument for the function is the other Carbon instance
to diff against, and of course it defaults to now() if not specified.
update
If the timestamp from the database being passed to diffForHumans is in the future, Carbon automatically makes the return like:
When comparing a value in the future to default now:
1 hour from now
5 months from now
When comparing a value in the past to another value:
1 hour before
5 months before

Related

Laravel : calculate before inserting into DB

I have a form and before saving it in the DB is it possible to make a calculation?
I get a date of birth and want to make an account with the current year.
Example: 10-10-1990 and I want it to subtract from the current year.
1990 - 2021
$tabel->datenasc = $request->datenasc;
You can still do your calculations before inserting into DB
// if $request->datenasc == 10-10-1990
$current_year = date("Y");
list($day, $month, $year) = explode("-", $request->datenasc);
$year_diff = $current_year - $year;
you can now insert $year_diff in the database;
You can use setYear method from carbon library
$date = Carbon::parse("10-10-1990");
$dateWithCurrentYear = $date->setYear(now()->format('Y'));

How to check if month change?

I want to update all my depriciation cost each month for all items until 0
Example.
Depriciation has value of 111 . each month it should be depreciated until depriciation cost
reach 0 or less than
controller
all_item=assets::all();
foreach($all_item as $item)
{
//should i make static month to compare?
$month=Carbon::now()->format('m');
$update_item = assets::findOrFail($item->id);
$update_item->depriciation_cost = $item->depriciation - $item->depriciation_cost;
$update_item->update();
}
You can create a new column in the table to keep track of the last depreciation value updated like depreciation_updated_at (type: date) OR use created_at/updated_at column to get the last update month (if no other reason to update record).
Then simply use updated version of your code:
all_item=assets::all();
foreach($all_item as $item)
{
$month=Carbon::now()->format('m');
$depreciation_updated_at = createFromFormat('Y-m-d', $item->depreciation_updated_at); //or can use created_at/updated_at as said below
if ($depreciation_updated_at ->format('m') != $month) {
$update_item = assets::findOrFail($item->id);
$update_item->depriciation_cost = $item->depriciation - $item->depriciation_cost;
$update_item->update();
}
}
Hope it will do your task.

Input date value and compare with existing table

I'm trying to develop a program with Laravel. I have created a table ‘tbl_holiday’ like below:
database table
I have created a form containing 02 input (date) types named ‘from_leave’ and ‘to_leave’.
I want to check these 2 inputs value(date) with the database table ‘tbl_holiday’ having the condition:
If the previous day of ‘from_leave’ match with ‘tbl_holiday.holiday_date’
Or next day of ‘to_leave’ match with ‘tbl_holiday.holiday_date’
The message “Invalid”.
Else
Message “Valid”.
Thanks in advance.
You can simply use wherebetween and check the number of data available in the collection.
$from = $request->from_date;
$to = $request->to_date;
$dates = YourModel::whereBetween('holiday_date', [$from, $to])->get();
if($dates->count() == 0){
return "valid";
}else{
return "Invalid";
}

Laravel Eloquent - Update Enum type

I have an enum column in my table Schedule. I want to be update the column, but I cannot. My type consists of 3 options: common, template, revision.
This does NOT work. The process goes through fine, but the type does not change:
$schedule = Schedule::find($id);
$schedule->type = 'common';
$scehdule->save();
return $schedule;
But this DOES work:
$schedule = Schedule::find($id);
$schedule->fill(['type' => 'common']);
$scehdule->save();
return $schedule;
This also works:
$schedule = Schedule::find($id);
unset($schedule->type);
$schedule->type = 'common';
$scehdule->save();
return $schedule;
Problem is that I have validators that need to be satisfied if I were to use fill(). I only want to change one column, so I'd like to use the first approach.
Could someone advice?
I ran into the same problem with Laravel 5.3. When you are using an enum in the database you can set the enum by using an integer. 1 = common, 2 = template, 3 = revision.
$schedule = Schedule::find($id);
$schedule->type = 1;
$scehdule->save();
return $schedule;
This will set the schedule type to "common"

codeigniter - save current date

I need to save current date and I supposed I must modify this code into application\libraries\grocery_crud.php
line # 253
case 'date':
/*if(!empty($value) && $value != '0000-00-00' && $value != '1970-01-01')
{
list($year,$month,$day) = explode("-",$value);
$value = date($this->php_date_format, mktime (0, 0, 0, (int)$month , (int)$day , (int)$year));
}
else
{
$value = '';
}*/
$value = // some code with current date
It's in that way? or maybe there is another solution, hope can help me, thanks in advance!
Sometimes you may not be able to change the column type from datetime to timestamp.
On these scenarios, considering a GroceryCrud application, there are two possible (and very easy) solutions:
1) Use callback_insert, create your custom insert logic and set the value for the datetime column using a function like NOW() or GETDATE() depending on the DB your are using: $this->db->set('dateTimeColumnName', 'NOW()', FALSE);
2) You can use the callback_before_insert, and inside it, you can call the function date() from PHP, and set there the current date for your insert in the respective array or object attribute that will be passed / returned for the auto-insert be processed right after the callback´s execution

Resources