Tag Archives: PHP

PHP – String to DateTime and vice versa

// Assume you have a date string which is in d-m-Y format.
$date_string = '18-04-2013';

// Create the DateTime object
$date = date_create_from_format('d-m-Y', $date_string);

// Print the date in Y-m-d format.
print date('Y-m-d', $date->getTimestamp());

 

Done =)

Reference:

Advertisements

PHP – http:// wrapper is disabled in the server configuration by allow_url_fopen=0

If you find these 2 errors in the error log.

  • …file_get_contents() [function.file-get-contents]: http:// wrapper is disabled in the server configuration by allow_url_fopen=0 in…
  • …failed to open stream: no suitable wrapper could be found in…

 

Edit the php.ini and enable the following 2 settings.

allow_url_fopen = On
allow_url_include = On

 

Restart Apache and they should be fixed.

Done =)

Reference:

Drupal 7 – Pass values from PHP to Javascript

During Drupal development, sometimes we may need to manipulate some server data in Javascript. In this case, we could create a custom module to retrieve the database data and pass it to browser such that we can it it in Javascript.

1. Implement the hook_init().

function <module-name>_init() {
  drupal_add_js(
    array(
      'eureka' => array( // we should use module name for best practice
        'data' => array(
          'Name' => 'ykyuen',
          'Blog' => 'Eureka!',
          'URL'  => 'https://eureka.ykyuen.info/',
        ),
      )
    ),
    'setting'
  );
}

Continue reading Drupal 7 – Pass values from PHP to Javascript

Run Drupal 7 in Nginx

After so many days we have talked about setting up Nginx, PHP-FPM, MariaDB and some PHP caching. We can now try to run a Drupal instance on them. Before we starts, let me listed out all the previous posts.

 

It’s time to start the Drupal installation.
Continue reading Run Drupal 7 in Nginx

Ubuntu – Install Memcached with PHP-FPM and Nginx

Previous: Ubuntu – Install APC with PHP-FPM and Nginx

Memcached is a distributed memory caching system and it caches data and objects in RAM to reduce the number of times an external data source (such as a database or API) must be read.

Like APC, the installation is simple.

1. Install Memcached.

  • apt-get install memcached

Continue reading Ubuntu – Install Memcached with PHP-FPM and Nginx