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')))
Related
I have been trying to get the the Year and Month from the user and insert them into the database.
i am using input type month so that the user can send only the Year and the Month.
<input type="month" name="date_from"/>
<input type="month" name="date_to"/>
and this is my model
function setDateFromAttribute($value)
{
$this->attributes['date_from'] = \Carbon\Carbon::createDateFromFormat('Y-m', $value);
}
function setDateToAttribute($value)
{
$this->attributes['date_to'] = \Carbon\Carbon::createDateToFormat('Y-m', $value);
}
protected $fillable = [
'date_from',
'date_to',
];
and data is saved as 0000.00.00
the data type in my database for these two inputs is
timestamp
i do not know what i am doing wrong here. please help
I don't see createDateFromFormat as a valid Carbon method in the documentation https://carbon.nesbot.com/docs/
Likewise, there does not appear to be any createDateToFormat method.
Carbon::createFromFormat() returns a Carbon object, not a string or equivalent MySQL timestamp, so, assuming you change your code to be:
$this->attributes['date_from'] = \Carbon\Carbon::createFromFormat('Y-m', $value)->toDateTimeString();
It should provide the results you are looking for.
References:
Converting a carbon date to mysql timestamp.
#Ted Stresen-Reuter
Sorry for the late reply couldn't find a solution that works within the model. tried your method which i have tried before with minor changes.
thanks a lot for trying to help me.. i found a method to complete this inside the controller which i will not recommend, but this was the best i was able to find which works and i was on a tight schedule.
'date_from' => isset($date_from[$key]) ? date('Y-m-d h:i:s', strtotime($date_from[$key])) : '',
'date_to' => isset($date_to[$key]) ? date('Y-m-d h:i:s', strtotime($date_to[$key])) : '',
the type timestamp accepts at least dates in full formats. as i know how i pass the data from blade to the controller i was able to add a date method and within it a strttotime method to convert my YY-MM into YY-MM-DD and the date that is inserted by default will be 01.
Sometimes date stored in my database looks like this: 0000-00-00.
This is how I store date in my controller:
$add_order->current_date = date('Y-m-d');
change the data type of your column to timestamp
then use this in your controller:
$add_order->current_date = \Carbon\Carbon::now();
use
$add_order->current_date = \Carbon\Carbon::now()->format('Y-m-d);
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');
I want to keep the time record of changes in certain columns of my table. How can I get and save timestamp values in db? I am trying to do it in following way but getting always 0000-00-00 00:00:00 in col_edited_at column.
$case->col_edited_at = time();
$case->save();
The other answers are correct but I have an idea of a more elegant solution. So you don't have to think about setting the timestamp anymore. Add this to your model and if you change the column it will refresh the col_edited_at field:
public static function boot(){
parent::boot();
static::saving(function($model){
if($model->isDirty('col')){
$model->col_edited_at = Carbon::now();
}
});
}
So when the model is getting saved and col is dirty (meaning it has changed) the timestamp will be set to the current time.
Usage:
$model = MyModel::find(1);
$model->col = 'foo';
$model->save();
MySQL timestamp fields by default require the format 0000-00-00 00:00:00. If you're just wanting to store a unix style "seconds since the epoch" timestamp like that returned by time() use an integer field, or else use:
$case->col_edited_at = date('Y-m-d H:i:s');
$case->save();
simply use
date('Y-m-d H:i:s') instead of time()
I have set mysql database field type as 'datetime'
when i send
$now = time();
echo form_hidden('a_date', unix_to_human($now));
from views all I get in database is 0000-00-00 00:00:00
please help
i usually do it like this
$now = date('Y-m-d H:i:s', strtotime('now'));
echo form_hidden('a_date', $now);
If you want to output differently, use http://www.php.net/manual/en/function.date.php
Also, if you want to record the time and not update it, I would recommend a timestamp field instead of datetime field. That way, you don't have to pass the db anything