how to use date mutator for laravel date conversion - laravel

I have a problem with inserting date formate
laravel use 'Y-m-d H:i:s' formate
whereas I have this format coming from admin panel m/d/Y
now, how can I convert date coming m/d/Y format to 'Y-m-d H:i:s' formate.

This should work, assuming a $request is passed from form submit.
$date = Carbon\Carbon::createFromFormat('m/d/Y', $request->date_field)->format('Y-m-d H:i:s');

Related

How format give me error. not give me proper date

my parsing date format is in date/month/year (21/12/2020).
use carbon/Carbon
$date = Carbon::parse($request->date)->format('Y-m-d');
return $date;
Please help.
You can use createFromFormat of DateTime. first of all, you need to convert this string invalid date format to date format by using createFromFormat
then you may format it in real date format.
$date='21/12/2020'; //dd/mm/yy
$new=DateTime::createFromFormat('d/m/Y', $date )->format('Y/m/d');
echo $new;
see example
Edit:-
By using carbon.
$date='21/12/2020';
$newdate = Carbon::createFromFormat('d/m/Y',$date)->format('Y/m/d');
return $newdate;

Set input default date format for dates in a model

There is a way to set the default date format of input dates in a model?
I need to create a model with data received in JSON format and date are formatted as Y-m-dTH:i:sP, but date in the DB are stored in the default Y-m-d H:i:s format. I added all the date fields in the $dates property of the model, but now when i create the model setting all the params in the constructor, like this:
$model = new Model($params);
I get a Carbon conversion error (InvalidArgumentException) because it tries to create a Carbon object from the Y-m-d H:i:s format.
I know Date mutators, but if i set
protected $dateFormat = "Y-m-d\TH:i:sP";
it creates the Carbon object correctly but i get an error while storing in the DB, because it tries to store the record with that format.
Is there a standard way to achieve what I need without converting the date format on each date param?
//date format = Y-m-dTH:i:sP
$date = '2011-09-16T11:38:23-05:00';
$new_date = date_format(date_create($date), 'Y-m-d H:i:s');

Laravel Query Builder - Where date is now using carbon

How to get only date on database column, I have difficulties using Carbon on controller:
$data['nowUser'] = User::where('date', Carbon::today()->toDateString())->get();
Date column looks like this in the database:
That is a DATETIME column, so there's no need for additional formatting the Carbon instance. However you need to use whereDate if you want the fetch all users for which the date column contains today's date:
$data['nowUser'] = User::whereDate('date', '=', Carbon::today())->get();
Because when you pass Carbon::today() to the Query Builder method, the __toString method will be automatically called and return a DATETIME string with the format from Carbon::DEFAULT_TO_STRING_FORMAT, which is exactly that MySQL format Y-m-d H:i:s.

Convert unix timestamp to normal Date in Json

I am using laravel 5.0. I am getting data from controller as json. I am having value like this.
{"timstamp_date":"1434360957"},
I need to convert this unix timestamp value as Normal Date Like (15-06-2015) or (15-March-2015).
I have used Date(timstamp_date) but it is showing current time only. Not my timstamp date
You could use:
date("d-m-Y H:i:s", 1434360957);
EDIT
You could try;
var dateTime = new Date(1434360957*1000);
var formatted = dateTime.toGMTString();
https://jsfiddle.net/sp57Lnpf/
Use the date function. You need to specify the format as the first parameter:
date("d-m-Y", $timestamp_date)
http://php.net/manual/en/function.date.php
Laravel also comes with Carbon you could use that if you wanted to for further manipulation of the data if you so required it.
http://carbon.nesbot.com/docs/

Laravel 4 timstamp

I have a field in mysql table with type 'TIMESTAMP' and using a calendar on my view to get date in format dd/MM/YYYY. Inserting data with following code
$billing = new Billing;
$billing->date = Input::get('date');
$billing->save();
It is filling data with 0000-00-00 00:00:00 in my field. How can i get correct value. I also tried to convert it to timestamp as followed.
$billing = new Billing;
$billing->date = date("Y-m-d H:i:s",strtotime(Input::get('date')));
$billing->save();
But it is also not working...Can any one help!!!
if I understand you correctly, mysql timestamps have to be saved as strings in YYYY-mm-dd HH:ii:ss format (ie, as returned by date('Y-m-d H:i:s')). You could convert with date('Y-m-d H:i:s'), strtotime(Input::get('date')))

Resources