Laravel 5 date Format value - laravel

I would like to know why my code is not working.
I have my model Organigramme i want to calculate with a function the age between two dates:
public function getAncienneteAttribute()
{
$now = Carbon::now();
$date_dentree = Carbon::createFromFormat('d/m/Y', $this->date_dentree);
return $now->diffInYears($date_dentree);
}
But I'm faced this error: Data missing
Someone can help me please.

Your $this->date_dentree format is not matching the format you passed to Carbon::createFromFormat, it is probably the default format so try the code below:
public function getAncienneteAttribute()
{
$now = Carbon::now();
return $now->diffInYears(new Carbon($this->date_dentree));
}

Try this
$date_dentree = \Carbon\Carbon::createFromFormat('Y-m-d', $this->date_dentree);
$diffYears = \Carbon\Carbon::now()->diffInYears($date_dentree );
//\Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $request->date)->format('Y-m-d')
$dbDate = \Carbon\Carbon::parse('2020-05-10');
$diffYears = \Carbon\Carbon::now()->diffInYears($dbDate);

Related

How to convert 2021-04-10 to 2021-04-11 increase day+1 in Laravel

public function search_sale(searchRequest $request) {
return $request->date2;
}
When return it, it is showing 2021-04-10 but I want it to show 2021-04-11. How can I do it?
You can use Carbon, check below
use Illuminate\Support\Carbon;
public function search_sale(searchRequest $request) {
$date = Carbon::createFromDate($request->date2)->addDay();
return $date->toDateString();
}
You can also add multiple days e.g. ->addDays(3)
You can use strtotime() function to modify the date.
public function search_sale ( searchRequest $request ){
$date = $request->date2;
return date('Y-m-d', strtotime($date . ' +1 day'));
}
Take a look at the documentation to learn more strtotime() function.

How to fix error in codeigniter4 and date_format?

I am making this query in codeigniter4 and I am stuck
public function consultasHoy(){
$data = $this->consultas->select('pacientes.*,consultas_hc.*')
->from('consultas_hc')
->join('pacientes','consultas_hc.identificacion = pacientes.identificacion')
->where(date_format('consultas_rc.reConsulta','%Y-%m-%d'),date_format(now(),'%Y-%m-%d'))
->orderBy('consultas_hc.reConsulta', 'ASC')
->get();
return json_encode($data);
}
and it returns this error
date_format() expects parameter 1 to be DateTimeInterface, string
given
If you are getting date straightforwardly from the database to your controller then you can print date format in view like below:
$YourDate = $row->start_date;
$newDate = date("d-m-Y", strtotime($YourDate));
Try replacing your where clause with this line of code:
$where="DATE_FORMAT('consultas_rc.reConsulta','%Y-%m-01')= DATE_FORMAT(CURDATE() ,'%Y-%m-01')";
->where($where);
this was the solution used, thanks for the help
public function consultasHoy(){
helper('date');
$hoy=(new \DateTime("now"))->format("Y-m-d");
$builder = $this->consultas->select('*');
$builder->join('pacientes', 'pacientes.identificacion=consultas_hc.identificacion');
$builder->where("DATE(reConsulta)", $hoy);
$query = $builder->get();
return json_encode($query->getResult());
}

Return multiple values in laravel

I'm just new in laravel. I want to know. how to return multiple data/value.
public function readItems() {
$data1 = Data1::all ();
$data = Data::all ();
return $data;
}
I'm quite confuse how to do it. I don't to return it as a view, i just want to return only the data. i hope someone could help. thanks a lot..
You could return an array like :
return [data1, $data];
In the other side read it like :
$response = readItems();
$data1 = $response[0];
$data = $response[1];
You can send data to view as like below :
return view('index', ['Data_One'=>$data, 'Data_Two'=>$data1]);

Codeigniter: Extract certain value from an array under controller

Good day!
This is pretty basic to some.
I have this code on my controller:
$data["sales_info"] = $this->The_model->get_sales_info(SELECT * FROM sales WHERE sales_id = 123);
Then get_sales_info() method under The_model stores the specific sales information into an array.
Now, I'm doing some debugging, so from the controller, I want to extract from the $data["sales_info"] and echo only the value of 'sales_date'.
How am I going to do this?
Thank you!
You can try this solution for your problem:
Change the model file to :
The_model.php
class The_model extends MY_Model {
function get_sales_info($sales_id){
$this->db->select("*.*");
if(!empty($sales_id)){
$this->db->where('sales_id', $sales_id);
$query = $this->db->get('sales');
return $query->row();
} else {
$query = $this->db->get('sales');
return $query->result();
}
}
}
and change the controller file to:
My_controller.php
$sales_id = 123;
$sales_info = $this->The_model->get_sales_info($sales_id);
echo $sales_info['sales_date'];exit; //how to get value from object
Thanks
This code may help you
$sales_info = $this->The_model->get_sales_info(SELECT * FROM sales WHERE sales_id = 123);
$data['sales_data'] = $sales_info['sales_date'];
$query = $this->db->get_where('sales', array('sales_id' => 123));
It should be like this, you cannot write the sql in to a method as best practice.

Laravel- How to check difference between two time stamps using Carbon

I'm currently working in laravel 5.4 and I need to calculate difference between two available timestamps using Carbon Class. But I'm getting this error Call to a member function diffInHours() on string any insights from you people would be helpful thank you :)
User Controller
class UserController extends Controller
{
use EncryptDecrypt;
public function resetPassword($token)
{
$decryptTS = trim($this->decryptText($token));
$split = explode('-', $decryptTS, 2);
$userId = $split[0];
$timeStamp = $split[1];
$timeStamp1=Carbon::createFromTimestampUTC($timeStamp)->toDateTimeString();
$now = Carbon::now();
if($timeStamp1->diffInHours($now) <=24)
{
echo "valid URL";
}
else
{
echo "Invalid URL";
}
}
}
Try to replace this line :
$timeStamp1=Carbon::createFromTimestampUTC($timeStamp)->toDateTimeString();
with :
$timeStamp1=Carbon::createFromTimestampUTC($timeStamp);
this first one will return string not a carbon object

Resources