Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Tuesday, October 30, 2007

PHP Date Math with date() and mktime()

Normal date math in PHP does not account for leap years (or apparently Daylight Savings Time for that matter).

If the current month is January, 2008 and you run
$newMonthText = date("F", mktime(0, 0, 0, date("m")+1, date("d"), date("Y")));
you'll get "March" for the value instead of what you probably want, which is "February." The good people on php.net have lots to say about this. (I think the other condition is that "skipping" Feburary happens only on the 30th or 31st of the current month.)

The way to deal with this is to use
$newMonthText = date("F", mktime(0, 0, 0, date("m")+1, 1, date("Y")));
which will return "February" as intended.

Monday, June 4, 2007

Getting a Count of Posts in The Loop in WordPress

I had a hard time finding this information. Here's how you can get a count of the number of posts in your current loop and the index of the current post you're accessing. post_count starts at zero and current_post starts at 1. In this example, I'm outputting a horizontal rule after all posts except the last one in the loop.

<?php
/* From http://codex.wordpress.org/The_Loop_in_Action */
if( ($wp_query->current_post + 1) < ($wp_query->post_count) )
{
echo("<hr />");
}
?>

Sunday, May 27, 2007

Web Services in PHP 4

I have a situation where I need to use a web service in PHP 4. I found a piece of free PHP code call nusoap and in this site created /lib/nusoap for all of its code. Here are some helpful snippets to access the web service methods and data.
require_once('lib/nusoap/nusoap.php');
$wsdl = "http://service.domain.com/ws.asmx?WSDL";
$client = new soapclient($wsdl, 'wsdl');
$proxy = $client->getProxy();
$authResponse = $proxy->authenticate_token(array('token_id'=>$cToken, 'site_code'=>$WS_SITE_CODE));
if($authResponse['authenticate_tokenResult'] == 'true')
{
setcookie("mytoken", $cToken, 0, "/", ".mydomain.com");
// Modified to work on www and non-www usage of the domain.
}
$authInfoResponse = $proxy->authenticate_passive(array('ip'=>$cIP, 'referrer'=>$cRef));
$authArrayResponse = $authInfoResponse['authenticate_passiveResult'];
$token_id = $authArrayResponse['token_id'];
if ($token_id != "0" && $token_id != "")
{
setcookie("mytoken", $token_id, 0, "/", ".mydomain.com");
}

Friday, April 27, 2007

Simple XML Parsing with PHP 4

Here's a very simple way to parse a small XML file with PHP 4 using the DOMXML module. This approach only makes sense since the file I'm working with is very small.
$qsOption = "";

// Make sure the file exists.
if ( !$dom = domxml_open_file("C:\path\to\file\file.xml") ) {
//echo "Error while parsing the document\n";
//exit;
// We'll exit quietly instead.
}
else
{
$root = $dom->document_element();
$nodes = $dom->get_elements_by_tagname("topic");
$element = new DomElement();
foreach($nodes as $node)
{
$element = $node;
$qsOption = $qsOption . "<option value=\"" .
$element->get_attribute("tid") . "\">" .
$element->get_content() . "</option>";
}
}

PHP .ini File with IIS on Windows Server 2003

I had to install PHP 4 under IIS on Windows Server 2003. The manual installation went fine, but PHP was looking for the php.ini file in C:\WINDOWS. I already added my install directory, C:\php, to the PATH environment variable and rebooted the machine, but this didn't seem to matter.

Buried on one of the many posts on php.net, I found a suggestion that worked. I had to add the following registry key (Start > Run > regedit)...
HKEY_LOCAL_MACHINE\SOFTWARE\PHP
...with the string value name IniFilePath and the value c:\php. After I did this, I did a hard stop of IIS and restarted it and the change took fine and saw the proper php.ini file.
C:\>iisreset /stop
C:\>net start w3svc

Monday, March 5, 2007

Set "Week of" Date in PHP

As of this writing, I'm very much a PHP newbie, but here's a snippet for setting a "Week of" date where Monday is zero day and the date format is along the lines of Monday March 5, 2007.

function getWeek()
{
$today = date("l"); // That's a lower case "L."
$shift = "0";

if($today == "Sunday") $shift = "-6 days";
elseif($today == "Monday") $shift = "0 days";
elseif($today == "Tuesday") $shift = "-1 day";
elseif($today == "Wednesday") $shift = "-2 days";
elseif($today == "Thursday") $shift = "-3 days";
elseif($today == "Friday") $shift = "-4 days";
elseif($today == "Saturday") $shift = "-5 days";

return date("l F j, Y", strtotime($shift, strtotime(date("l F j, Y")))); // "Ls"
}

echo getWeek();

Monday, February 26, 2007

phpMyAdmin and the "Client does not support authentication" Error

When you see the following error...
#1251 - Client does not support authentication protocol requested by server; consider upgrading MySQL client
...there is a problem with the password you created for a user. You'll need to run...
mysql> SET PASSWORD FOR 'username'@'host' = OLD_PASSWORD('password');
See http://dev.mysql.com/doc/refman/5.0/en/old-client.html for more information.