Wednesday, November 28, 2018

finding difference between two date in php and javascript

Javascript code:

<!DOCTYPE html>
<html>
<title>Web Page Design</title>
<head>
<script>
function sayHello() {
//var date1 = new Date("11/28/2018");
//var date2 = new Date("12/1/2018");

var date1 = new Date("2018-11-28");
var date2 = new Date("2018-12-01");
var timeDiff = Math.abs(date2.getTime() - date1.getTime());
var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));
alert(diffDays);

}
sayHello();
</script>
</head>
<body>
</body>
</html>

PHP code:

$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");

OR

$startDate = strtotime('2018-12-01');
$toDay = strtotime(date('Y-m-d'));
$time = abs($toDay - $loginDate);
$days = floor($time / 86400);


Wednesday, November 21, 2018

Laravel : Laravel cheet sheet

Cakephp : Cakephp Cheet sheet

A CakePHP cheat sheet (CakePHP reference page)

Summary: This is a CakePHP cheat sheet. (Note: This reference was initially created in 2011, and may be slightly out of date.)
As I embark on another CakePHP project after a long hiatus, I'm trying to cram CakePHP back into my head. As part of this effort, I'm creating this large CakePHP cheat sheet (reference page), which I hope will be helpful to the CakePHP community. It is based on both the CakePHP cheat sheet on their Trac website and the CakePHP Cookbook (see the links below).
(Go ahead and say what you will about me, but I happen to like all my CakePHP examples on one big page like this that I can search, rather than spread out across many different pages. That's what a cheat sheet is, right? Also, please note that there are very few ads on this page, besides the standard ads on the side columns.)

Monday, November 19, 2018

sms gatway integration code

CONS SMS_USER = 'UserName';
CONS SMS_PASS = 'P@nrb';
CONS SMS_HOST = 'http://sms.sslwireless.com/pushapi/dynamic/server.php';
$phone = '88' . $phone;
$text = 'You have ' . $total . ' new Tenders of your preferred area. For details, please check your email or visit agent result at http://bdtender.com. Helpline: 01789-772266';

$params = 'user=' . self::SMS_USER . '&pass=' . self::SMS_PASS . '&sms[0][0]=' . $phone . '&sms[0][1]=' . urlencode($text) . '&sms[0][2]=880123456789&sid=bdtender';

$ch = curl_init(self::SMS_HOST);

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $params);

$response = curl_exec($ch);
curl_close($ch);

return $response;

Wednesday, November 14, 2018

Point of sale : Open Source porject list

Database : Single-column index vs. multicolumn index

The two types of indexes are single-column indexes and multicolumn indexes. A single-column index is an index based on the values in one column of a table. A multicolumn index is an index based on the values in multiple columns of a table.
Organization of this subsection
(1) Creating a single-column index
(2) Creating a multicolumn index

(1) Creating a single-column index

A single-column index should be created when retrieval will be executed using one column only as the key.

(2) Creating a multicolumn index

A multicolumn index should be created in the cases discussed below.
(a) Retrieval of data that satisfies multiple conditions
A multicolumn index should be created when data satisfying multiple conditions is to be retrieved, such as when a complex-condition retrieval using the AND operator with multiple columns as the key is executed.

http://itdoc.hitachi.co.jp/manuals/3020/3020635200e/W3520279.HTM#ID00988

Database : Guidelines for Application-Specific Indexes

You can create indexes on columns to speed up queries. Indexes provide faster access to data for operations that return a small portion of a table's rows.
In general, you should create an index on a column in any of the following situations:
  • The column is queried frequently.
  • A referential integrity constraint exists on the column.
  • UNIQUE key integrity constraint exists on the column.
You can create an index on any column; however, if the column is not used in any of these situations, creating an index on the column does not increase performance and the index takes up resources unnecessarily.
Although the database creates an index for you on a column with an integrity constraint, explicitly creating an index on such a column is recommended.
You can use the following techniques to determine which columns are best candidates for indexing:
  • Use the EXPLAIN PLAN feature to show a theoretical execution plan of a given query statement.
  • Use the V$SQL_PLAN view to determine the actual execution plan used for a given query statement.

Sometimes, if an index is not being used by default and it would be most efficient to use that index, you can use a query hint so that the index is used.

Thursday, November 8, 2018

CakePHP : Implementing the Repository Pattern with CakePHP

Implementing the Repository Pattern with CakePHP


I must admit, my recent articles are becoming a bit obsessed around the repository pattern.  What can I say, I like it, it’s useful, and it’s not restrictive based on a language or a framework.

I’ve long professed how I dislike convoluted controllers.  CakePHP’s find method almost immediately causes this when used inside a controller.  More importantly, the code inside the find method is extremely unreadable.  This is almost more important than a large controller function!

This is where the repository pattern comes in.  At its most basic example (which some will consider overkill – you know who you are), I still think the repository pattern is clearer.

Here is an example using the regular find approach:
$user = $this->User->find('first', array('conditions' => array('id' => $id)));

Compared to a repository example:
$user = $this->UserRepository->GetById($id);

The code is almost identically; however, in the second example, it’s clear that if I were to “read” the code I am retrieving a user by id opposed to I’m finding the first user with the conditions of id being equal to the variable $id.

http://www.endyourif.com/implementing-the-repository-pattern-with-cakephp/

OOP : Traits

Summary: in this tutorial, you will learn how to use PHP traits to share functionality across independent classes, which are not in the same inheritance hierarchy.

Introduction to PHP traits

Code reuse is one of the most important aspects of object-oriented programming. In PHP, you use inheritance to enable code reuse in different classes that share the same inheritance hierarchy. To achieve code reuse, you move the common functionality of classes to method of the parent class. Inheritance makes the code very tightly coupled therefore makes the code hard to maintain.
To overcome this problem, as of version 5.4.0,  PHP introduced a new unit of code reuse named  trait.Traits allow you to reuse a set of methods freely in many different classes that does not need to be in the same class hierarchy.
Trait is similar to class but it is only for grouping methods in a fine-grained and consistent way. It is not allowed to instantiate a trait on its own.

http://www.zentut.com/php-tutorial/php-traits/

OOP : interface

Summary: in this tutorial, you will learn how to use PHP interface that is one of the most important building blocks in PHP object-oriented programming.

Introduction to PHP interface

An interface allows you to specify a list of methods that a class must implement. To define an interface, you use the interface keyword as follows:
An interface consists of methods that contain no implementation. In other words, all methods of the interface are abstract methods. An interface can have its own constants. See the following syntax:
All the methods in the interface must have public visibility level.

How to access stdclass object after a specific key value pair?

PHP : Dynamic Properties in PHP and StdClass

Languages like JavaScript and Python allow object instances to have dynamic properties. As it turns out, PHP does too. Looking at the official PHP documentation on objects and classes you might be lead to believe dynamic instance properties require custom __get and __set magic methods. They don't.
Simple, Built-in Dynamic Properties
Check out the following code listing:
class DynamicProperties { }
$object = new DynamicProperties;
print isset($object->foo) ? 't' : 'f'; // f

// Set Dynamic Properties foo and fooz
$object->foo = 'bar';
$object->fooz = 'baz';

// Isset and Unset work
isset($object->foo); // true
unset($object->foo);

// Iterate through Properties and Values
foreach($object as $property => $value)  { 
     print($property . ' = ' . $value . '<br />'); 
}

// Prints:
//   fooz = baz
Using the built-in dynamic instance properties is an order of magnitude faster (30x, by my profiling) than using magic __get and __set methods. Built in dynamic property accesses happen without invoking a method call back to PHP script.
So when does it make sense to use __get and __set? If you need more complex behavior, like calculated properties, you must use __get and __set. Also, as an astute comment points out, if you would prefer not to have dynamic properties on a class you can throw errors from __get and __set.

http://krisjordan.com/dynamic-properties-in-php-with-stdclass

Javascript: Understand JavaScript Closures With Ease

What is a closure?
A closure is an inner function that has access to the outer (enclosing) function’s variables—scope chain. The closure has three scope chains: it has access to its own scope (variables defined between its curly brackets), it has access to the outer function’s variables, and it has access to the global variables.
The inner function has access not only to the outer function’s variables, but also to the outer function’s parameters. Note that the inner function cannot call the outer function’s arguments object, however, even though it can call the outer function’s parameters directly.
You create a closure by adding a function inside another function.

javascript : What is a Callback or Higher-order Function?

What is a Callback or Higher-order Function?

A callback function, also known as a higher-order function, is a function that is passed to another function (let’s call this other function “otherFunction”) as a parameter, and the callback function is called (or executed) inside the otherFunction. A callback function is essentially a pattern (an established solution to a common problem), and therefore, the use of a callback function is also known as a callback pattern.
Consider this common use of a callback function in jQuery:
//Note that the item in the click method's parameter is a function, not a variable.
//The item is a callback function
$("#btn_1").click(function() {
  alert("Btn 1 Clicked");
});
As you see in the preceding example, we pass a function as a parameter to the clickmethod. And the click method will call (or execute) the callback function we passed to it. This example illustrates a typical use of callback functions in JavaScript, and one widely used in jQuery.
Ruminate on this other classic example of callback functions in basic JavaScript:
var friends = ["Mike", "Stacy", "Andy", "Rick"];

friends.forEach(function (eachName, index){
console.log(index + 1 + ". " + eachName); // 1. Mike, 2. Stacy, 3. Andy, 4. Rick
});
Again, note the way we pass an anonymous function (a function without a name) to the forEach method as a parameter.
So far we have passed anonymous functions as a parameter to other functions or methods. Lets now understand how callbacks work before we look at more concrete examples and start making our own callback functions.

https://javascriptissexy.com/understand-javascript-callback-functions-and-use-them/



Wednesday, November 7, 2018

How can I use my own external class in CakePHP 3.0?

What are the naming conventions? Do I need to use a specific namespace?
Your SVG graphs class should have a namespaces. For namespaces you can see http://php.net/manual/en/language.namespaces.rationale.php
Where do I put the file that contains the PHP class?
  1. Create a folder by author(here might be your name, as you are the author) in vendor
  2. Then create your class inside of it convention is vendor/$author/$package . You can read more http://book.cakephp.org/3.0/en/core-libraries/app.html#loading-vendor-files
How can I include it and use it in a controller or a view?
a) To include:
require_once(ROOT .DS. 'Vendor' . DS . 'MyClass' . DS . 'MyClass.php');
(replace MyClass by your foldername and MyClass.php by your filename.php)
b) To use it:
add use MyClass\MyClass; in your controller
For example I want to add MyClass in a controller. Steps that worked for me
  1. Creating vendor\MyClass folder
  2. Pasting MyClass.php in that folder
  3. adding namespace MyClass; at the top of MyClass.php
MyClass.php have following code for example:
namespace MyClass;


class MyClass
{
    public $prop1 = "I'm a class property!";

    public function setProperty($newval)
    {
        $this->prop1 = $newval;
    }

    public function getProperty()
    {
        return $this->prop1 . "<br />";
    }
}
  1. Adding use MyClass\MyClass; at the top of controller
  2. Then including it in my controller action. My action sample
       public function test()
     {
         require_once(ROOT .DS. "Vendor" . DS  . "MyClass" . DS . "MyClass.php");
    
         $obj = new MyClass;
         $obj2 = new MyClass;
    
         echo $obj->getProperty();
         echo $obj2->getProperty();
         exit;
     }