formatter asDateTime in yii2 - yii2

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.

Related

How to update a document in MongoDb with the field value itself in iterations?

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

Laravel format DateTime from database result

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.

Jira JSON date format

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.

jqGrid Doesn't Sort When Showing Epoch Time (since as milliseconds) as Date

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.

Is my JSON correct?

I've implemented a JSON and an RSS feed into FullCalendar [only currently pulling events from JSON]. Using CodeIgniter I've created my JSON using the following code
<?php
$jsonevents = array();
foreach($events->result() as $entry){
$jsonevents[] = array(
'id' => $entry->eventID,
'title' => $entry->eventTitle,
'start' => $entry->startDate,
'end' => $entry->endDate,
'allDay' => false
);
}
echo json_encode($jsonevents);
?>
The following outputs this JSON
[{"id":"1234567891","title":"Test 3","start":"2011-05-07 00:00:00","end":"2011-05-13 00:00:00","allDay":false},
{"id":"24242","title":"sdfsdfsdfsdf","start":"2011-05-05 13:00:53","end":"2011-05-06 17:00:19","allDay":false},
{"id":"1234567890","title":"Test","start":"2011-05-05 13:00:53","end":"2011-05-06 17:00:19","allDay":false}]
The issue is when imported into FullCalendar it creates an event within an event, I believe it might have something to do with the square brackets, but I'm unsure of how to correct the output, anyone have any ideas?
Your json is right.
I've validated it with http://json.bloople.net/
You may have another problem when applying it to your calendar plugin.
I've just formatted your json string with JSON Formatter & Validator and it's valid.
The square brackets mean that the whole thing is an array. Please refer to json.org.
Json is right.. yay.
I also had this problem ;)
Make all day True and look below on the time.
2011-05-07 00:00:00
try notice the 'T'
2011-05-07T00:00:00
Here is a sample of my complex feed
[{"title":"Tee Times","start":"2011-05-03T00:00:00","end":"2011-05-03T00:00:00","allDay":true,"color":"rgb(21,144,51)","groupsize":"","className":"data-brs clickable","cache":null,"EventName":null,"description":null,"EventCompTypeMSP":null,"url":null,"CompName":null,"CompCourseName":null,"CompNumberDivs":null,"CompQualifierYN":null,"CompNumber":null},{"title":"Tee Times","start":"2011-05-04T00:00:00","end":"2011-05-04T00:00:00","allDay":true,"color":"rgb(21,144,51)","groupsize":"","className":"data-brs clickable","cache":null,"EventName":null,"description":null,"EventCompTypeMSP":null,"url":null,"CompName":null,"CompCourseName":null,"CompNumberDivs":null,"CompQualifierYN":null,"CompNumber":null},{"title":"Tee Times","start":"2011-05-05T00:00:00","end":"2011-05-05T00:00:00","allDay":true,"color":"rgb(21,144,51)","groupsize":"","className":"data-brs clickable","cache":null,"EventName":null,"description":null,"EventCompTypeMSP":null,"url":null,"CompName":null,"CompCourseName":null,"CompNumberDivs":null,"CompQualifierYN":null,"CompNumber":null},{"title":"Tee Times","start":"2011-05-06T00:00:00","end":"2011-05-06T00:00:00","allDay":true,"color":"rgb(21,144,51)","groupsize":"","className":"data-brs clickable","cache":null,"EventName":null,"description":null,"EventCompTypeMSP":null,"url":null,"CompName":null,"CompCourseName":null,"CompNumberDivs":null,"CompQualifierYN":null,"CompNumber":null},{"title":"","start":"2011-05-03T07:10:00","end":"2011-05-03T08:00:00","allDay":false,"color":"rgb(21,144,51)","groupsize":"","className":"data-brs clickable","cache":null,"EventName":null,"description":null,"EventCompTypeMSP":null,"url":null,"CompName":null,"CompCourseName":null,"CompNumberDivs":null,"CompQualifierYN":null,"CompNumber":null},{"title":"Test","start":"2011-05-04T07:00:00","end":"2011-05-04T07:50:00","allDay":false,"color":"rgb(21,144,51)","groupsize":"","className":"data-brs clickable","cache":null,"EventName":null,"description":null,"EventCompTypeMSP":null,"url":null,"CompName":null,"CompCourseName":null,"CompNumberDivs":null,"CompQualifierYN":null,"CompNumber":null},{"title":"Riley","start":"2011-05-04T08:00:00","end":"2011-05-04T08:00:00","allDay":false,"color":"rgb(21,144,51)","groupsize":"4","className":"data-brs clickable","cache":null,"EventName":null,"description":null,"EventCompTypeMSP":null,"url":null,"CompName":null,"CompCourseName":null,"CompNumberDivs":null,"CompQualifierYN":null,"CompNumber":null},{"title":"Test","start":"2011-05-04T08:10:00","end":"2011-05-04T17:50:00","allDay":false,"color":"rgb(21,144,51)","groupsize":"","className":"data-brs clickable","cache":null,"EventName":null,"description":null,"EventCompTypeMSP":null,"url":null,"CompName":null,"CompCourseName":null,"CompNumberDivs":null,"CompQualifierYN":null,"CompNumber":null},{"title":"pea","start":"2011-05-05T08:10:00","end":"2011-05-05T08:10:00","allDay":false,"color":"rgb(21,144,51)","groupsize":"1","className":"data-brs clickable","cache":null,"EventName":null,"description":null,"EventCompTypeMSP":null,"url":null,"CompName":null,"CompCourseName":null,"CompNumberDivs":null,"CompQualifierYN":null,"CompNumber":null},{"title":"peter","start":"2011-05-06T07:10:00","end":"2011-05-06T07:10:00","allDay":false,"color":"rgb(21,144,51)","groupsize":"1","className":"data-brs clickable","cache":null,"EventName":null,"description":null,"EventCompTypeMSP":null,"url":null,"CompName":null,"CompCourseName":null,"CompNumberDivs":null,"CompQualifierYN":null,"CompNumber":null},{"title":"March Medal - Mens Medal - CSS: 70 - PAR: 70 - Click for more info...","start":"2011-03-15T00:00:00","end":null,"allDay":null,"color":"rgb(10,87,164)","groupsize":null,"className":"data-previous clickable","cache":null,"EventName":null,"description":null,"EventCompTypeMSP":null,"url":"http://www.howdidido.co.uk/ClubDetails.aspx?section=24&pagesection=compresultsdetails&compid=383","CompName":"March Medal","CompCourseName":"White Tee's","CompNumberDivs":1,"CompQualifierYN":"N","CompNumber":383}]