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');
Related
I am creating a Laravel application. Earlier I used to design the database with the datetime datatypes for created_at and updated_at, but a friend of mine suggested me that I should use timestamp instead of datetime because it's good with different timezones.
Is this a good idea to use timestamp instead of datetime format? Will there be any performance issues? If "No" then how can we change the default format of timestamps from datetime to timestamp globally in a laravel application.
For example (default):
$model->created_at = "yyyy-mm-dd h:i-s";
$model->updated_at = "yyyy-mm-dd h:i-s";
Integer timestamp:
$model->created_at = 1523246567;
$model->updated_at = 1523246567;
Unfortunately, I'm not able to answer your first two questions, but Laravel does provide an easy way to change the format your dates are stored in.
The Date Mutators documentation says that one can set the $dateFormat property on your model like this:
/**
* The storage format of the model's date columns.
*
* #var string
*/
protected $dateFormat = 'U';
The 'U' format would be "Seconds since the Unix Epoch." More formats are available in the php date documentation.
You would also need to change your model's migration. Replace $table->timestamps(); with $table->unsignedInteger('created_at'); and $table->unsignedInteger('updated_at');.
To use a specific date format "globally", i.e. for all of your models, you could either set the date format on a base model and let all of your models inherit that model, or use a trait. See this question for an example.
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');
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.
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/
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')))