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

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

Related

Laravel - different formats for date/time query

I am learning Laravel and I have some small problem on controllers - when I use DB, the query returns date time without timezone but if I use model, the query returns full datetime.
public function test($switch)
{
//return "YYYY-MM-DDThh:mm:ss.000000Z"
if ($switch) return Position::select('id','created_at')->orderBy('id')->get();
// return "YYYY-MM-DD hh:mm:ss"
return DB::table('positions')->select('id','created_at')->orderBy('id')->get();
}
Why? What I need to dof I want "YYYY-MM-DDThh:mm:ss.000000Z" on both cases?
Thanks for solution.
thanks for advice
You should use the DB::raw
The following statement will convert the datetime value created_at from +00:00 timezone to +10:00 timezone.
You can try this
return DB::table('positions')->select(DB::raw('id',CONVERT_TZ('created_at','+00:00','+10:00'))->orderBy('id')->get();
you can set your timezone that you wants to convert it
They are the same data, probably just different classes of date and you can always format your date. Laravel utilizes Carbon date library which is excellent and should be used primarily.
If you try to print out your date class with get_class() for Eloquent Position created_at, you probably got Carbon and DB::table('positions') created_at, you probably got DateTime and that's why the value looks different (but you still got the same date).
If you want to convert your DateTime to Carbon, you can do
$newDate = new \Carbon\Carbon($position->created_at)
Thanks Anurat,
I realized this fact shortly after sending the previous question.
... but there is another 'issue' - both times are my local time - time in "YYYY-MM-DDThh:mm:ss.000000Z" is not UTC time as I expected.
I changed my function:
public function test($switch = false)
{
$data = Position::selectRaw('id, created_at, UNIX_TIMESTAMP(created_at) unix')->orderBy('id')->get();
foreach ($data as $d) {
$conv = new \DateTime($d->created_at);
$d->conv = intval($conv->format('U'));
$d->diff = $d->conv - $d->unix;
}
return $data;
}
... and result is
0
id 1
created_at "2021-03-18T12:36:59.000000Z"
unix 1616067419
conv 1616071019
diff 3600
As you see, the difference is 1 hour (as my timezone offset). Where is a problem?
Thanks.

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.

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

date time problem with 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

Resources