How format give me error. not give me proper date - laravel

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;

Related

Error when using the format: $table->date('column_name') in Laravel

I'm having some trouble with date formats in Laravel.
I have a database migration which has a field that looks like the following:
$table->date('Date');
Then, in the controller, I try to fill this field in with the following content:
2019-04-15
But I get the following error:
invalid datetime format: 1292 Incorrect date value: '"2019-04-15"' for column 'Date' at row 1
Any help on which would be the correct way of declaring the field 'Date' in the database would be apreciated.
Thank you very much in advance.
It's to do with strict date typing in MySQL I believe.
Check these answers for more information.
https://stackoverflow.com/a/47946950/1848660
https://stackoverflow.com/a/48998959/1848660
Try using Carbon to generate dates and timestamps.
This will generate a timestamp for the current date and time.
Carbon\Carbon::now();
Or you can parse your current date and convert it to a Carbon stamp
Carbon\Carbon::parse($date)->toDateTimeString();
Documentation:
https://carbon.nesbot.com/docs/
This works with your case:
$date = "2019-04-15";
$formatted_date = Carbon::createFromTimestamp(strtotime($date))->format('Y-m-d');
You can go ahead and test your insert. I have tested this. You won't face the 1292 Incorrect date ERROR.
DB::table('yourTable')->insert(['date'=> $formatted_date]);
Try with this,
$table->date ? date('Y-m-d', strtotime($table->date)) : '-';
You should convert your date to string and then apply date function on that for different date format.

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

how to use date mutator for laravel date conversion

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

Laravel 5: can't set date from string

On my db migration i have a "dob" field:
$table->date('DOB')->nullable();
I have a string representing a date: "12/12/1960" in the input. I tested "dob" and the content is there. But when I try to set it to the "dob" field in the database..
$member->DOB = date('m/d/Y',Request::input('dob'));
The datebase field becomes 0000-00-00
What am I doing wrong?
The database most likely wants it in YYYY-MM-DD format. So just changing the format should work. But to get from 12/12/1960 to there, you need to parse it.
$oldDateString = '12/12/1960';
$newDateString = DateTime::createFromFormat('m/d/Y', $oldDateString)->format('Y-m-d');
Look at definition
string date ( string format [, int timestamp] )
Second parameter is timestamp, not string
Use DateTime PHP5 functions or Laravel Carbon
E.g. date_create_from_format('m/d/Y', Request::input('dob'))
->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/

Resources