background image
HomeRecent PostsDrupalSearchTagsRSSContactAboutAccount
Eric.London's picture

I just encountered a PHP fatal error when running my cron.php:

Fatal error: Call to undefined function timezone_open() in /MYSERVERPATH/httpdocs/sites/all/modules/date/date_api.module on line 607

A quick Google search, and I found the issue is documented here. The solution is to enable the Date PHP4 module. But, this issue does not happen in our production environment, so I decided to compare PHP versions:

# on the production server:
$ php -v | head -1
PHP 5.2.8 (cli) (built: Dec  9 2008 14:03:11)

It turns out, a fully updated installation of Centos 5.2 only supplies PHP 5.1.x. So, I decided to upgrade PHP in my development environment according to this documentation.

I created a new yum repo file:

$ sudo emacs /etc/yum.repos.d/utterramblings.repo

# FILE CONTENTS - START
[utterramblings]
name=Jason's Utter Ramblings Repo
baseurl=http://www.jasonlitka.com/media/EL$releasever/$basearch/
enabled=1
gpgcheck=1
gpgkey=http://www.jasonlitka.com/media/RPM-GPG-KEY-jlitka
# FILE CONTENTS - END

And, ran another yum update:

$ sudo yum update
# ...snip...
Resolving Dependencies
# ...snip...
Dependencies Resolved

=============================================================================
Package                 Arch       Version          Repository        Size
=============================================================================
Updating:
apr                     i386       1.2.12-2.jason.1  utterramblings    257 k
apr-util                i386       1.2.12-5.jason.1  utterramblings    159 k
httpd                   i386       2.2.8-jason.3    utterramblings    2.5 M
mod_ssl                 i386       1:2.2.8-jason.3  utterramblings    314 k
mysql                   i386       5.0.58-jason.2   utterramblings    6.4 M
mysql-server            i386       5.0.58-jason.2   utterramblings     10 M
pcre                    i386       7.6-jason.1      utterramblings    562 k
php                     i386       5.2.6-jason.1    utterramblings    3.7 M
php-cli                 i386       5.2.6-jason.1    utterramblings    2.6 M
php-common              i386       5.2.6-jason.1    utterramblings    481 k
php-devel               i386       5.2.6-jason.1    utterramblings    568 k
php-gd                  i386       5.2.6-jason.1    utterramblings    320 k
php-ldap                i386       5.2.6-jason.1    utterramblings     56 k
php-mbstring            i386       5.2.6-jason.1    utterramblings    1.3 M
php-mssql               i386       5.2.6-jason.1    utterramblings     61 k
php-mysql               i386       5.2.6-jason.1    utterramblings    258 k
php-odbc                i386       5.2.6-jason.1    utterramblings    112 k
php-pdo                 i386       5.2.6-jason.1    utterramblings    159 k
php-pear                noarch     1:1.6.2-1.jason.1  utterramblings    418 k
php-soap                i386       5.2.6-jason.1    utterramblings    342 k
php-xml                 i386       5.2.6-jason.1    utterramblings    316 k
php-xmlrpc              i386       5.2.6-jason.1    utterramblings    130 k
subversion              i386       1.4.4-jason.1    utterramblings    4.3 M

Transaction Summary
=============================================================================
Install      0 Package(s)        
Update      23 Package(s)        
Remove       0 Package(s)        

Total download size: 35 M
Is this ok [y/N]:

After updating all these packages, I checked out my new PHP version:

$ php -v | head -1
PHP 5.2.6 (cli) (built: May  5 2008 10:32:59)

Now, my PHP fatal error has been resolved :)

Eric.London's picture

At some point, you might want to pass date ranges as arguments to a view. Unfortunately, the built-in functionality of views does not allow you to use a date argument as a range. If you try to specify the date arguments by editing your view, the views module will automatically insert SQL to match the dates exactly, which will not work in this scenario. Assuming that you already have a view and a content type that has two fields for start and date ranges, you can add a hook_db_rewrite_sql() function to add your own SQL to the view's query.

<?php
// define the db_rewrite_sql hook:
function MYMODULE_db_rewrite_sql($query, $primary_table = 'n', $primary_field = 'nid', $args = array()) {
 
// search for the view
 
if (is_object($args['view']) && $args['view']->name=='MYVIEWNAME') {
   
// if there are no view arguments, don't bother continuing     
   
if (!is_array($args['view']->args)) return;

   
// get the start and end date ranges from the view arguments
    // NOTE: if you don't know which arguments are which,
    // you can use: print_r($args['view']->args)
   
$startDate = $args['view']->args[0];
   
$endDate = $args['view']->args[1];

   
// validate dates. exit function if issue
   
if (strlen($startDate) && !checkdate(date('m', strtotime($startDate)), date('d', strtotime($startDate)), date('Y', strtotime($startDate)))) return;
    if (
strlen($endDate) && !checkdate(date('m', strtotime($endDate)), date('d', strtotime($endDate)), date('Y', strtotime($endDate)))) return;

   
// create var for where clause
   
$where = "";

   
// define table alias
   
$tableAlias = 'MYALIASNAME';

    if (
$startDate) {
     
$where = " {$tableAlias}.field_start_date_value >= '$startDate' ";
    }

    if (
$endDate) {
      if (
strlen($where)) $where .= " and ";
       
$where .= " {$tableAlias}.field_end_date_value <= '$endDate' ";
    }

    if (
strlen($where)) {
      return array(
'join' => "inner join {content_type_MYCONTENTYPE} $tableAlias on node.vid = $tableAlias.vid and $where");
    }
   
  }

}
?>

Now, you can pass the date ranges in as arguments using the views_embed_view function:

<?php
$viewName
= 'MYVIEWNAME';
$arg = array('2008-01-01','2008-12-31');
$viewsHtml = views_embed_view($viewName, 'default', $arg);
?>

Eric.London's picture

I recently wrote a quick BASH shell script to FTP a log file to another server monthly. First, I modified the logrorate configuration to rotate a service's logs monthly. Then I added a cron job to be executed the following script once a month. NOTE: It's important to give logrotate enough time to finish rotating the logs. Here's my script:

#!/bin/bash

_user="MYFTPUSER"
_password="MYFTPPASSWORD"

# create a date string in the format YYYYMM for last month
_date=$(date +%Y%m --date="-1 month")

# Create FTP connection and put the log in the user's home folder
ftp -n MYFTPSERVER <<EOF
user $_user $_password
binary
put /var/log/MYROTATEDLOG.log.1 ~/MYROTATEDLOG.$_date.log
bye
EOF

Here's how you can use a few third party moves to implement a calendar date picker. Install and enable the following modules... CCK, date (http://drupal.org/project/date), and Javascript Tools (http://drupal.org/project/jstools). In Javascript Tools, you want to enable jscalendar. First, create a new CCK node type (or edit an existing node type). Then, click on add field, and choose date: Text Field with javascript pop-up calendar. When editing or adding the node, you get a calendar icon next to your text input field. When clicking on it, you'll get an interactive calendar...

Syndicate content