Friday, May 26, 2017

PHP strtotime() does not work with dd/mm/yyyy Format

Hello,

This is quick blog regarding PHP strototime function. In our recent project we were importing data from CSV where we faced two issues.

1) If we try to import date like 01/05/2016 considering dd/mm/yyyy format, it saves date in database as 2015-01-05

2) If we try to import date like 31/08/2016 considering dd/mm/yyyy format, it saves data in database as 1970-01-01

In short month and day is swiped while saving. So in case of date like 31/08/2016,  31 is assumed as month and there is no such month in calendar so it gave error and returns date like 1970-01-01

This is first time we faced such issue. I was not sure what's the exact issue but I assumed it has something to do with separator. So we tried same dates with  - instead of / and it worked. So to solve this temporary, we had following solution.

$date = '31/08/2016';
$date = str_replace('/', '-', $date);
echo date('Y-m-d', strtotime($date));

So we replace / with - and it worked but solving problem is not enough, we shall go in deep to check why we had this problem so curiously I looked into function documentation of strtotime and found out following.

"Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. "

So indeed the problem here was with the separator.

Hope this helps you.

Wednesday, May 24, 2017

How to exit the Vim editor?

Every time I use vi editor on terminal in OSX or Linux, I face issue on exiting Vim editor on terminal. So in this blog I am going to explain complete procedure on exiting vim editor on terminal in OSX or Linux.

What is Vim editor?

Vim is a highly configurable text editor built to make creating and changing any kind of text very efficient. It is included as "vi" with most UNIX systems and with Apple OS X.

How to Use Vim on Terminal?

Go to terminal and type following command

vi "NameOfFile"

and it will open Vim editor like this.



This is blank file. To start editing file press "I" key and then you can start editing file.

Now the tricky part is exiting vim editor on terminal. Here are exact procedure.

Hit the Esc key, vim goes into command mode.

Type Shift key + : and you will see : at bottom of the file.

Now you have to type command to tell editor what you want to do with the file. Here are some options you have.

q to quit
q! to quit without saving
wq to write and quit
wq! to write and quit even if file has only read permission
x to write and quit
qa to quit all
Now press "Return key" and it will exit vim editor.

Saturday, May 20, 2017

React Native undefined is not an object (evaluating 'this.props.navigator.push)

Hello,

Recently we have been working with React Native for one of our application where we faced an issue with Android Navigator. Basically we had two lists Category List and Sub Category List and on tap of category list item we wanted to render sub category list. Following is the code for that.

<TouchableOpacity onPress={()=> this.pressRow(rowData)} >
<CardSection>
 <View style={thumbnailContainerStyle}>
<Image
 style={thumbnailStyle}
 source={{ uri: rowData.image }}
/>
 </View>
 <View style={headerContentStyle}>
<Text style={headerTextStyle}>{rowData.name}</Text>
 </View>
</CardSection>
</TouchableOpacity>

And following is function where we were using navigator.push to go to next view.

pressRow (rowData) {

    if(rowData.subcategory == true)
    {
      this.props.navigator.push({
        name: SubCategoryList,
        passProps: {rowId:rowData.id}
      });
    }
    else if(rowData.subcategory == false)
    {
      this.props.navigator.push({
        name: ProductList,
        passProps: {rowId:rowData.id}
      });
    }

 }

Here we are getting problem with error, this.props.navigator is undefined so were not able to go to second screen.

The problem here is on scope as we were having category list rendered in app container like this.


<CategoryList />

So props of parent are not passed to child and hence navigator was not available. To solve this all you have to do it add your child component like this.

<CategoryList  navigator={this.props.navigator} />

And now navigator object will be available. Hope this solves your problem.