date time problem with codeigniter 2 - codeigniter-2

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

Related

How to insert date format YY-MM into database?

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.

Date is stored like this 0000-00-00 in database

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

How to set model attribute to current date format using MySQL functions?

I have a model Users And every time user logges in loggedin_at field is updated.
$user = User::find(1);
$user->token = md5(time());
$user->loggedin_at = date('Y-d-m H:i:s');
$user->save();
return $user;
But I know from experience already that sometimes there is difference between MySQL time and PHP time. So when you use comparisons like time > NOW() - INTERVAL 1 HOUR it might now work correctly because you set date from PHP and compare it from MySQL.
Anyway my question is I want to use NOW() to update my date. I try
$user->loggedin_at = DB::raw('NOW()');
But that does not work. By looking into Eloquent source I've managed out this to work
$user->loggedin_at = new \Carbon\Carbon();
This is what Eloquent uses to alter times.
How to use NOW() to set time?
Should I use NOW() or better continue with Carbon?
It's better to use Carbon, it's what it's there for. Laravel uses it out of the box, and uses it within created_at and updated_at.
If you use Carbon, also, you can synchronize PHP's time and Carbon's "NOW" using the timezone configuration inside of config/app.php.
Furthermore, if you want to change the result you get from Carbon, you can do something like:
$now = Carbon::now(new DateTimeZone('Europe/London'));
//can also be passed as a string
$now = Carbon::now('Europe/London');

Custom Timestamps in Laravel

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()

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