RSS

  • YouTube
  • LinkedIn
  • Google

Archives for : Development

Using jQueryMask Plugin to format dates in different patterns

jQuery Mask Plugin is a great Javascript library to format fields for presentation and/or to force a properly input by users.

Here, I show how to deal with input or another HTML element to display dates when the source date has a different format from target element.

Example:

  • We have a datetime with Year-Month-Day Hour:Minute:Seconds and need to show only the Year/Month/Day part.
  • We have da date in Year-Month-Day and need to format to Day/Month/Year.

jQueryMask is very simple to use. It do not need to much to mask whatever you need. Take a look at examples on his website.

If you need to format a simple date field (even in a non-input element), just use the code below.

The code above can mask and pre-validate dates in YEAR/MONTH/DAY format.

With an input like

would be changed his value to 2018/06/11.

If you try to type another date (after clear, of course) you cannot begin with a number different from 1 or 2. This reason is that our mask have a translation to be done when allowing chars. If the pattern does not match, the char is erased.

The meaning of “TC99/M9/D9” mask is:

  • Must begin with 1 or 2 (Translation T: /[12]/ – Custom format)
  • Must have number 0 or 9 (Translation C: /[09]/ – Custom format)
  • Must have a number (any) (Translation 9 – MaskPlugin Core format)
  • Must have a number (any) (Translation 9 – MaskPlugin Core format)
  • A slash will be added automatic ( / )
  • Must have number 0 or 1 (Translation M: /[01]/ – Custom format)
  • Must have a number (any) (Translation 9: MaskPlugin Core format)
  • A slash will be added automatic ( / )
  • Must have number 0, 1, 2 or 3 (Translation D: /[0-3]/ – Custom format)
  • Must have a number (any) (Translation 9: MaskPlugin Core format)

Of course, there is no real validation. You can type “2999/19/39” and this is not a valid date, but is almost done.

So, to format in another way, just change mask parameter order.

But, if the source date is in a different pattern, like MONTH/DAY/YEAR, the mask do not work. The date output for “06/11/2018 15:40” will be weird “1018/15/0“.

To handle different date formats will be needed more than simple mask. We will need a function.

Look the code below

Now we have two more Translation Pattern (h and m). h means that the n-index position must have numbers 0, 1 or 2 and m numbers between 0 and 5. Keep in mind that CASE matters.

With the above code, we can format and show date in several ways. just change .test() and .replace() pattern to fill your desired pattern.

This is the code that I am using to format Database datetime fields with YEAR-MONTH-DAY HOUR:MINUTE:SECOND in html elements with DAY/YEAR/MONTH HOUR:MINUTE

With an input like

Output will be 11/06/2018 15:40

Hope this help you!

Binding several values in PDO SQL Statement (PDOStatement)

Sometimes we need to search a single value through several columns when building SQL instructions.

Suppose the table below

  • [ENTITY_ONE]
    • ID
    • COLUMN_A
    • COLUMN_B
    • COLUMN_C
    • COLUMN_D
    • COLUMN_E

If we need search a single value on columns B,D and E we will need use the following instruction

In PHP code we can do

Well, this can work but we know that isn’t the best approach. We need use Binding Values to avoid SQL injection and other malicious treats.

So, the code can be modified to

Much better, but, when building complex SQL instruction, things can be confusing with lots of arguments and don’t forget: ORDER MATTERS.

Happily PDO can bind values in different order when using named bindings.

Hmm, seems that this isn’t good enough. We only change the use of 1-indexed placeholder to a :named placeholder. There’s no gain beyond of code readable and the possibility to bind in any order.

Yes, but now we can do the best approach when using one unique search term in several columns. We can use only one bind to one or more :named placeholders ’cause PDO is smart and clever. Look our final code here.

Can save a lot of typing when writing many SQL instruction using same argument.

Naming files using list from 0 to Z

Today I was coding some scripts and found a little trouble to use a defined pattern.

The pattern is to create files where the sequence starts in 0 (zero) and cannot be repeated until Z.

Example:

myfile0.ext, myfile1.ext, myfile2.ext, (...), myfile9.ext, myfileA.ext, myfileB.txt, (...), myfileZ.txt

Well, this is not a big trouble so I did use this code.

But $seq did not gave the expected value of 0 (zero) on first run. Instead, it was blank.

Debugging the variables, I saw that the while never evaluates to true. Attempting to reproduce on command line I saw that in_array($seq, $seqs); always return true. I tryed to use “”, “R” and no matter what value I used, still returning true.

So I change to use STRICT argument for in_array to true and works for ‘A’ through ‘Z’, but not for ‘0’ through ‘9’.

Damn… PHP is right, “0” is not strictly equals to 0. The Chr function return string and range('0', '9') creates an array with integer values.

So, I changed the approach to evaluate all values with STRICT, because I would like to create a nice and clean code without no other functions to be used.

This is the final code that I’m using:

How you can see, I changed the $seqs initial values from ‘0’ to your ASCII code and get back to your value that gave me an array with all values in string type.

See you!

PHP Comparison Error

Today, I was writing a script in PHP to be used in the command line when I came across unexpected behavior (at least on my part).
The script should receive three arguments, the last of them a list containing one or more numeric codes.
Trying to validate this last argument was getting a different result than imagined.
See the code snippet that I was using:

Continue Reading >>

jQuery.html() doesn't work in Internet Explorer

I recently had problems with a javascript code that uses jQuery and it worked in several browsers i.e. Chrome, o Firefox and some versions of Internet Explorer, but that stubbornly refused to work in Internet Explorer 7 and 8.
Here is the code:

After some tests I realized that wasn't a problem in the method, as in other codes it behaved as expected.
So debugging code thoroughly and the answers of the Ajax method realized a small syntax error in HTML and was just correct it and IE 7 displayed correctly the answer.
Then follows the big tip, If you have problems with the method jQuery.html(), check first of all whether the content being written in the element is an html code without errors, because the validation in IE 7/8 will not allow write HTML with errors, no matter how small it is.

Installing Redmine in Nginx on Centos 6. X

In this article I will show you how to set up the Redmine version 2.5.2 (2014-07-06) on a Web server Nginx.

Redmine is a web application for project management. I met him some time ago in one of the companies where I spent and is an excellent free alternative for those who need to manage projects, whether or not development.

Nginx is a Web server that is gaining a lot of space in recent years due to his performance for large quantities of request.

Continue Reading >>

Converting xor, shr and shl from Delphi to PHP

I'm migrating a software made in Delphi to PHP and I came across a problem in a fucking Blessed encryption function.

I suffered a bit trying to rewrite the function does the complexity of the code and with the differences in the results.

The function makes use of operators XOR and SHR in Delphi version. The SHR was relatively easy thanks to experience with bitwise operators and how Delphi documentation says that the operator pulls off bits to the right

Continue Reading >>

How to decrease the size of VMDK virtual disks-part 3

If you want to read the part 1 This tutorial, Click here.
If you want to read the part 2 This tutorial, Click here.

In the previous item made a cloning a disk with a single partition. In the next lines I'll show you how I cloned a disk with a single partition primary and logic for two primary partitions and a logic with a mount point for the swap that originally was in a file.

NOTE: I did use information from this site, to solve problems with the new boot disk. Do as I, always cite your sources.

Using as a basis the information of part 1 This tutorial, We will add a new disk to the virtual machine.

Continue Reading >>

Como diminuir o tamanho de discos virtuais VMDK –; part 2

If you haven't read the part 1, You can access it here.

Continuing the tutorial, We will now start the cloning of disks. The first part of the cloning is a disk that does not contain the operating system. If you want to go straight to the cloning system bootable, You can skip this part and go straight to the part 3.

Time to start cloning. We start the virtual machine in terminal mode (for if you have graphical environment) and login as root or we become root with the command su –; to be able to execute the instructions of partitioning, formatting and cloning, respectively without the need for sudo all command.

In terminal type fdisk-l for list disks and partitions:

Note that our disks appear as being /dev/sda, /dev/sdb, /dev/sdc (I marked in red to make it more visible) and beside its size. That order the, b, (c) is the connection order on IDE port. As our record was included in the Secondary Slave and on Primary Slave is the CDROM (see image on the 1) He is the third system disk, IE, o /dev/sdc.
We will from now on to partition and format the drive. Type the command Fdisk/dev/sdc.

Continue Reading >>

Como diminuir o tamanho de discos virtuais VMDK –; part 1

Where I work we have a few database servers PostgreSQL in different virtual machines running on a VMWare Server. So far so good, It wasn't a detail that was bothering me. All of them using old versions as 8.1 and 8.2, While the current version is the 9.3.
I sought to know and found out that there are version 9.3 for Centos 6.5 that's what we're using for the new servers and decided then unify the three servers in one.

Se você que leu até aqui e pensou “;Unify servers?? That Buro! give zero for him!; know that it was thought and our reality is an appropriate solution.

That said, and with the dilemma resolved, I set out to create a VM using the Vagrant.

The Vagrant is an excellent tool to help us Devops. With the use of it can create machines that can be easily shared among the members of a team. I won't go into the details of its use, just to inform you that I used a Box of Centos 6.5 Basic.

Until here everything was going very well. Set up the VM and installed the software that would use, including PostgreSQL 9.3.

When I went to migrate the production base started some problems. The PGDATA directory, that's where PostgreSQL stores the data in tables, I was with more than 40 GB. Do a dumpall and a Restore on-the-fly was not an option and decided to extract the whole Bank to later restore the VM.

Continue Reading >>