My database stores two dates in the mysql DateTime format YYYY-MM-DD HH:MM:SS. When I get this data (with other strings etc), I want to convert it to another format, maybe DD.MM.YYYY HH:MM:SS and display it on my view in a table cell. My database dates are called date_begin and date_end.
Better, when I get this dates from database, convert it to DD.MM.YYYY format, separate the date and the time, store the time in a custom string ("HH1:MM1 - HH2:MM2") and bring both on my view.
How can I achieve this? I found some examples to convert on the view, not in the controller, but I think this is not good for MVC.
Not sure where you've gotten the impression that "formatting the date in the view is not good for MVC", because that's not a problem whatsoever.
If you're using Eloquent Models you can do it very easily:
1. Add the columns to the $dates property in your model class:
protected $dates = ['date_begin', 'date_end'];
This will ensure that the values get mutated to Carbon instances.
2. In your view files you can use the format method that Carbon offers like so:
<!-- To use only the date with the given format -->
{{ $item->date_begin->format('Y.m.d') }}
<!-- To use only the time with the given format -->
{{ $item->date_begin->format('H:i:s') }}
<!-- To use both date and time with the given format -->
{{ $item->date_begin->format('Y.m.d H:i:s') }}
There's no need to split the value in time and date, just show what you want from the DateTime value using whatever format you want.
If you're not using Eloquent models, then you can manually use Carbon to format your value like so:
{{ Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $item->date_begin)->format('Y.m.d') }}
I have used the PHP solution without Carbon:
Get the database date as you do (with the default format: YYYY-MM-DD HH:MM:SS) and when you print it on your view replace $item->date_begin
to:
date('d-m-Y H:i:s', strtotime($item->date_begin))
After that, when you save it on the database add on the DB update:
date('Y-m-d H:i:s', strtotime($request->date_begin))
protected $casts = [
'inicio' => 'datetime:Y-m-d\TH:i'
,'entrega' => 'datetime:Y-m-d\TH:i'
];
you could use it for the datetime-local field. greed you. like me if work for you
// attributes that should be cast to native types
protected $casts = [
'email_verified_at' => 'datetime'
];
// get formatted datetime string for email_verified_at
public function getEmailVerifiedAttribute()
{
if ($this->email_verified_at) {
$date = Carbon::createFromFormat('Y-m-d H:i:s', $this->email_verified_at, 'UTC');
return $date->setTimezone($this->timezone->name)->isoFormat('LLLL');
} else {
return null;
}
}
as Laravel document way.
Related
I have Json column that stores data like this :
[
{"date": "2021-02-24"},
{"date": "2021-02-20"},
{"date": "2021-04-22"},
]
and I would like to get the data which is only after today, closest thing i found is this :
$dates = Foo::whereJsonContains('dates', ["date => '2021-04-22']);
but this will search by exact date , how to search by greater than just like what we do with normal where like this :
$dates = Foo::where('date', '>', Carbon::now());
Yes, it is possible to do it directly from the database, but it won't be easy.
For example, from your data you can extract json data to get an array with
SELECT JSON_EXTRACT(your_field, '$[*].date') AS dates
Here you will get dates as an array of string and you have to cast each of the string to date and compare them with tomorrow's date.
Once you are able to do that, you can pass the sql command in DB::raw()
If I have an element array like this :
[PERKIRAAN_SELESAI] => 24/10/2016 09:38
Then I want to store into datetime format in mysql which is YYYY-MM-DD H:s,
=> 2016-10-24 09:38
How can Yii2 handle this,
Now, in beforeSave(), I use this :
$this->perkiraan_selesai = Yii::$app->formatter->asDateTime(strtorime($this->PERKIRAAN_SELESAI), "php:Y-m-d H:s" );
But, still not working.
Please.
First of all, I see 2 errors in your code:
2016-10-24 09:38 date looks like a Y-m-d H:i format, not Y-m-d H:s. Check PHP date function docs for interpretation of these letters.
strtorime must be strtotime, but I think this typo is not in original code, just here in post.
As for your problem, check this related question on SO. Seems that the problem is with handling of slash (/). Instead of replacing it with - manually, I found solution with native class DateTime much better.
Using plain PHP:
$date = \DateTime::createFromFormat('d/m/Y H:i', '24/10/2016 09:38')->format('Y-m-d H:i');
Using Yii2 formatter:
$date = Yii::$app->formatter->asDateTime(\DateTime::createFromFormat('d/m/Y H:i', '24/10/2016 09:38'), 'php:Y-m-d H:i');
Just replace this date with your value.
The value of $date is 2016-10-24 09:38 as expected.
I have a MongoDb document such as
{
"_id" : ObjectId("5731c75196dada69cd52419d"),
"businessName" : "Test",
"createdDate" : "2015-10-04 13:48:55",
"modifiedDate" : "2016-03-03 10:37:48"
}
If you notice the createdDate and modifiedDate column are in YYYY-mm-dd h:i:s format (Which is coming from my Mysql Db). I need to convert the dates into ISO format.
For this, I am using the below query .
db.users.update({"_id" : ObjectId("5731c75196dada69cd52419d")},
{$set : {"createdDate" : ISODate('2015-10-04 13:48:55')}} )
I am getting the desired result.
Now my question is if we are having lot of documents and need to update the field value then how can we do it in iterations in Mongodb itself.
Is that possible ? Updating a single document each time will be very time-consuming. Any help will be appreciated.
Firstly get created at values then convert it into ISODate store it your array and then pass that array to createdDate in $in operator.
You can use toISOString() of javascript to convert values.
db.getCollection('users').update({'_id':{$in:[ObjectId('574d61acf834e66681d37804'),
ObjectId('574d6185f834e66681d37803')]}},
{$set : {"createdDate" :{$in:['your converted date 1','your converted date 2']}},
{multi: true})
I am using the Jira API, and need the start and end dates for a sprint.
The JSON data I get back is :
{"jodaTimeZoneId":"Europe/Berlin","sprints":[{"id":5,"start":"13082015044305","end":"27082015044305",...
Normally, json returns the date in milliseconds, and you need to deserialize that.
Now however, I can clearly see the date (13-08-2015 & 27-08-2015) followed by some other numbers I don't care about. Is there anyway Angular can get the correct format using | date? Or any other way I can use?
When I use {{13082015044305 | date:'dd-MM-yyyy'}} it returns 21-07-2384. The parsing date format is wrong. So change the format to recognized way.
So I used
input.toString().replace(/(\d\d)(\d\d)(\d\d\d\d)(\d\d\d\d\d\d)/, '$1-$2-$3');
Used it in a custom filter.
app.filter('correctDateFormat', function() {
return function(input) {
return input.toString().replace(/(\d\d)(\d\d)(\d\d\d\d)(\d\d\d\d\d\d)/, '$1-$2-$3');
};
});
Then
Display the date as
{{13082015044305 | correctDateFormat }}
I think you can use
{{ data | filter:options }}
where data is your json and date filter
{{'1388123412323' | date:'MM/dd/yyyy # h:mma'}}
an option like this.
I use jqGrid and my grid definition is like that:
...
colNames:['Type','Date','Message','User Name','Host'],
colModel:[{name:'type',index:'type', width:100},
{name:'date',index:'date', sorttype:'date', formatter:'date',
formatoptions: {newformat:'d-M-Y'}, width:100},
{name:'log',index:'log', width:200},
{name:'username',index:'username', width:50},
{name:'host',index:'host', width:50}],
...
When I debug my coming data one of the date value (it is Number) is as follows:
1322550786997
Grid shows it like that:
29-Nov-2011
Everything is OK till this point. However when I want to sort my date column it doesn't change anything.
Any ideas?
The problem is that decoding of the Unix (formatoptions: {srcformat: 'U', newformat: 'd-M-Y'}) date 1322550786997 get us 19-Dec-43879 and not 29-Nov-2011. You correct representation of the date will be the string "\/Date(1322550786997)\/" instead of the number 1322550786997.
See the demo:
UPDATED: You can also use the following custom formatter as a workaround
formatter: function (cellval, opts) {
var date = new Date(cellval);
opts = $.extend({}, $.jgrid.formatter.date, opts);
return $.fmatter.util.DateFormat("", date, 'd-M-Y', opts);
}
It creates Date and then use original date formatter to convert it to the format 'd-M-Y'. See here the demo.