PHP time() and Mysql now() fucntion

The time () function is a built-in function in PHP . time() returns the current time measured in the number of seconds since the Unix Epoch. The number of seconds can be converted to the current date using date () function in PHP.

<?php
$t=time();
echo($t . "<br>");
echo(date("Y-m-d",$t));
?>

Output

1624962270
2021-06-29

The MySQL NOW() function returns the current date and time in the configured time zone as a string or a number in the 'YYYY-MM-DD HH:MM:DD'  format. The returned type of the NOW() function depends on the context where it is used. For example, in the following statement, the NOW() function returns the current date and time as a string.

select now();

output

PHP time() and Mysql now() fucntion













MySQL Thread Cache Size

 It's advisable to set thread_cache_size so that most new connections use threads from the cache rather than newly created threads. This saves some thread-creation overhead, though normally does not create a significant performance improvement.

To Set the Thread Cache Size Use Below Step

mysql> set global thread_cache_size = 16;

Query OK, 0 rows affected (0.00 sec)


or for a persistent change, edit my.cnf

thread_cache_size       = 16


Have a look at the variables:

mysql> show variables;

+--------------------------------------------------------+--------

| Variable_name                                          | Value

| thread_cache_size                                      | 16

Mysql : how to find the line number of error?

Depending how large the .sql file, you could use a program like notepad2 and open the it and it will have syntax highlighting to catch a possible quote mismatch. otherwise, break the statements out in to separate lines if possible. 

Instead of mysql> source mysource.sql # you can use command prompt with --verbose option for more helpful output.


# mysql --verbose test < mysource.sql

What is services in drupal 8

 In Drupal 8, a service is any object managed by the services container. Drupal 8 introduces the concept of services to decouple reusable functionality and makes these services pluggable and replaceable by registering them with a service container.

Service  is a PHP class with some code that provides a single specific functionality throughout the application. So you can easily access each service and use its functionality wherever you need it. Because of that it is easy to test and configure in your application. This is called service-oriented architecture which is not unique to Symfony or even PHP.  

The Services and Dependency Injection Container concepts have been adopted by Drupal from the Symfony framework.  Accessing the database, sending email, or translating user interface are examples for the services in Drupal 8. 

How to define your own service in Drupal 8  through custom module Step are below.

1-  create the .info.yml  file [custom_services.info.yml]

name: Custom Services

type: module

description: The module provide the sample code to define  your service from  your custom module.

core: 8.x

package: Custom


2-  create the .services.yml file [custom_services.services.yml]

services:

 custom_services.say_hello:

   class: Drupal\custom_services\HelloServices 

 File name is ‘custom_services.services.yml’ where the ‘custom_services’ is our module name.

‘custom_services.say_hello’ is the service name defined by us, , Where we need to follow the pattern of ‘module_name’ concatenate with a ‘unique_name’.

We have the class name for services. ‘class: Drupal\custom_services\HelloServices’ which will be kept under the ‘src’ folder.

Also the dependency can be added the  following way 

arguments: ['@modulename.services1', '@modulename.services4', '@modulename.services7']  

In this case there are no  dependencies.

For more details visit https://www.drupal.org/node/2194463

3- Create the class ‘HelloServices.php’ under the ‘src’ folder

<?php

/**

* @file providing the service that say hello world and hello 'given name'.

*

*/

namespace  Drupal\custom_services;

class HelloServices {

 protected $say_something;

 public function __construct() {

   $this->say_something = 'Hello World!';

 }

 public function  sayHello($name = ''){

   if (empty($name)) {

     return $this->say_something;

   }

   else {

     return "Hello " . $name . "!";

   }

 }

}


This is simple class provide the service.

How to access the our own defined service?

Accessing the service globally. 

$service = \Drupal::service('custom_services_example.say_hello');

If you want to test this , you can enable the devel module and go to the path ‘devel/php’ and  run the following code 

$service = \Drupal::service('custom_services.say_hello');

dsm($service);

dsm($service->sayHello('Waliullah'));

So you will get the following output.

Drupal\custom_services\HelloServices Object

(   

      [say_something:protected] => Hello World!   

      [_serviceId] => custom_services.say_hello

)

 

Hello Waliullah!


For  more details please visit  the following links


https://www.drupal.org/node/2133171

http://symfony.com/doc/current/book/service_container.html

How to find a position of character in a string in PHP without using strpos function

 In this blog you are going to see how to find a position of character in a string in PHP without using strpos function.

To find the position of the character in a string is a very simple and fast process if we going to use the string function called strpos function, but in this, we have to find the character of string without using strpos.

String Split function

The main purpose of the string split function is to split the string into an array. Below the syntax for string split.

str_split(string,length)

There are two parameters used in string split, one parameter is a string which we have to split and another parameter is the length of each array element and it is optional if it is not defined then the default is considered as 1.

Suppose we have string "contextual" , and we want to find the position of 't' in that string. follow the below step and code.


<?php

$str = "contextual";

$arr = str_split($str);

echo "<pre>";

print_r($arr);

foreach ($arr as $po => $st) {

if($st == 't') {

$data[] = $po; 

}

}

print_r($data);

?>


Output


How to find a position of character in a string in PHP without using strpos function


How to check the PHP version on Linux

 For checking  and validate PHP version on Linux follow below step

 Open a bash shell terminal and use the command “php –version” or “php -v” to get the version of PHP installed on the system.


# php --version

PHP 5.4.16 (cli) (built: Mar  7 2018 13:34:47) 

Copyright (c) 1997-2013 The PHP Group

Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies


# php -v

PHP 5.4.16 (cli) (built: Mar  7 2018 13:34:47) 

Copyright (c) 1997-2013 The PHP Group

Zend Engine v2.4.0, Copyright (c) 1998-2013 Zend Technologies


How to List Compiled PHP Modules from Command Line

If your server only has a single PHP version installed, you can run this PHP command anywhere, and it will give you the same list of modules. The general command we will be using is php -m. This command will give you the full list of installed PHP modules/extensions.

root@host [~]# php -m

This command will give you an output similar to the following information.

bcmath

bz2

Core

ctype

curl

date

dom

exif

fileinfo

filter

ftp

gd

hash

iconv

imagick

json

libxml

mbstring

mysqli

mysqlnd

openssl

pcntl

pcre

PDO

pdo_mysql

pdo_sqlite

Phar

posix

readline

Reflection

session

SimpleXML

SPL

sqlite3

standard

tokenizer

wddx

xml

xmlreader

xmlwriter

xsl

zip

zlib


[Zend Modules]






How I get the current and next month dates in PHP

 If you want to have the next month, I suggest you to use the current year and month but keep using the 1st.

Current Date 

$today = $today = date("Y-m-d"); 

Next Month Date

$date = date("Y-m-01");

$newdate = strtotime ( '+1 month' , strtotime ( $date ) ) ;

This way, you will be able to get the month and year of the next month without having a month skipped.

One month from today

$date = date('Y-m-d', strtotime('+1 month'));


One month from a specific date

$date = date('Y-m-d', strtotime('+1 month', strtotime('2015-01-01')));

PHP OOP Class

Like C++ and Java PHP also support object oriented programming.

Class are essentially user defined data types. Class are where we create a blueprint for the structure of methods and attributes. Individual objects are instantiated, or created from this blueprint.

Class is a programmer-defined data type, which includes local methods and local variables.

Class is a collection of objects. Object has properties and behavior.

Syntax - 

     Class is defined by using the keyword class followed by the name you want to give your new class.

<?php

   class Person {

   }

?>

Note - 

  We enclose a class using curly braces ( { } ) … just like you do with functions.


Example - 

   <?php 

          class Person {

                       public $name;

                       public $age;

                     // Method

                    function SetName ($name,$age) {

                      $this->name = $name;

                      $this->age = $age;

                     }

         }

    ?>

How do I declare a new template file in my Drupal 8 theme?

 Templates provide HTML markup and some presentation logic. Contrary to Drupal 7, in Drupal 8 template files (*.html.twig files) must be stored in the 'templates' subfolder.

You can instantiate new template files in the root of your custom theme folder simply by giving them the same name as one of the template files in core.

Core theme Bartik  template folder structure - 

 |-templates

  |  |-block--search-form-block.html.twig

  |  |-block--system-branding-block.html.twig

  |  |-block--system-menu-block.html.twig

  |  |-block.html.twig

  |  |-comment.html.twig

  |  |-field--taxonomy-term-reference.html.twig

  |  |-maintenance-page.html.twig

  |  |-node.html.twig

  |  |-page.html.twig

  |  |-status-messages.html.twig

HTML (<head> template )  - 

The HTML template provides markup for the basic structure of the HTML-page including the <head>, <title> and <body> tags.

Base template: html.html.twig (base location: core/modules/system/templates/html.html.twig)

The following are some examples of how you may override the base template:

html--[internalviewpath].html.twig
html--node--[nodeid].html.twig
html.html.twig

Page template - 

Pattern: page--[front|internal/path].html.twig
Base template: page.html.twig (base location: core/modules/system/templates/page.html.twig)


Regions - 

Pattern: region--[region].html.twig
Base template: region.html.twig (base location: core/modules/system/templates/region.html.twig)


Blocks - 

Pattern: block--[module|--[delta].html.twig
Base template: block.html.twig (base location: core/modules/block/templates/block.html.twig)


Nodes - 

Pattern: node--[content-type|nodeid]--[viewmode].html.twig
Base template: node.html.twig (base location: core/modules/node/templates/node.html.twig)

Theme hook suggestions are made based on these factors, listed from the most specific template to the least. Drupal will use the most specific template it finds:

node--[nodeid]--[viewmode].html.twig
node--[nodeid].html.twig
node--[content-type]--[viewmode].html.twig
node--[content-type].html.twig
node--[viewmode].html.twig
node.html.twig


Taxonomy Term - 

Pattern: taxonomy-term--[vocabulary-machine-name|tid].html.twig
Base template: taxonomy-term.html.twig (base location: core/modules/taxonomy/templates/taxonomy-term.html.twig)

Theme hook suggestions are made based on these factors, listed from the most specific template to the least. Drupal will use the most specific template it finds:

taxonomy-term--[tid].html.twig
taxonomy-term--[vocabulary-machine-name].html.twig
taxonomy-term.html.twig


Fields - 

Pattern: field--[[type|name]|[entity-type]--[field-name|content-type]].html.twig
Base template: field.html.twig (base location: core/modules/system/templates/field.html.twig)

Theme hook suggestions are made based on these factors, listed from the most specific template to the least. Drupal will use the most specific template it finds:

field--node--[field-name]--[content-type].html.twig
field--node--[field-name].html.twig
field--node--[content-type].html.twig
field--[field-name].html.twig
field--[field-type].html.twig
field.html.twig
Note that underscores (_) in a Field's machine name are replaced by hyphens (-). Also remember to include "field-" in custom field names, e.g: field--field-phone.html.twig.


Theme hook suggestions - 

ometimes you want to make changes to a template file, but only for some of the places that it's used. A common example is making changes to the node template file for only nodes of a specific type. Drupal's theme layer allows you to target specific use cases of a template file by following a specific naming convention. When rendering an article node Drupal will first look for the node--article.html.twig template file and use it if it exists. If it doesn't, Drupal will fall back to the default node.html.twig template. The process by which Drupal determines what possible names a template file could use is called theme hook suggestions.

Theme hook suggestions allow you to implement targeted overrides in your theme for template files with a specific naming convention.

All layers from core, modules, theme engines and themes can provide the suggestions. You can add or modify suggestions using the hooks:


hook_theme_suggestions_HOOK(array $variables)
hook_theme_suggestions_alter(array &$suggestions, array $variables, $hook)
hook_theme_suggestions_HOOK_alter(array &$suggestions, array $variables)




Drupal : How can I disable all the JavaScript scripts?

 You don't necessarily need to turn off aggregation if you want to remove all javascript. 

function mymodule_preprocess_html(&$variables) {
if (MODULE_get_headers()) {
unset($variables['#attached']['html_response_attachment_placeholders']['scripts']);
unset($variables['#attached']['html_response_attachment_placeholders']['scripts_bottom']);
}
$variables['#cache']['contexts'][] = 'headers:my-header';
}

You need a cache context for the request header. This doesn't work for the Internal Page Cache, though. So you need to uninstall this module or extend it to add the request header to the cache key.

Write a program in PHP to reverse a number

A number can be written in reverse order. For example 12345 = 54321 <?php   $ num = 23456;   $ revnum = 0;   while ($ num > 1)   {   $...