How-To: Create a mysql friendly date with php
This is really simple - and quite useful if you are trying to insert a date into mysql and you can’t use the “now()” mysql command (due to say a timezone difference):
$date = date(”Y-m-d G:i:s”);
This is really simple - and quite useful if you are trying to insert a date into mysql and you can’t use the “now()” mysql command (due to say a timezone difference):
$date = date(”Y-m-d G:i:s”);
Need to remove the trailing whitespace characters from a string in php? chomp() is how you do it in Perl, however in PHP it’s just as easy. Simply use rtrim():
// $string = “|my string with trailing whitespace |”
$string = rtrim($string);
// $string = “|my string with trailing whitespace|”
It’s that easy! There is [...]
An easy way to read an RSS feed with PHP is to use the MagpieRSS library. This provides a simple API to read an RSS feed, and elegantly deal with the issues of timeouts and caching.
It’s as simple as:
require(’rss_fetch.inc’);
$rss = fetch_rss($url);
echo “Site: “, $rss->channel['title'], “<br>
“;
foreach ($rss->items as $item ) {
$title = $item[title];
$url [...]
If you need to know the real host name of your server using php, use the following command:
<? echo gethostbyaddr (gethostbyname ($_SERVER["SERVER_NAME"])); ?>
This will return the server’s hostname, not the address that the script is being accessed by. Very useful if you have a script that needs to run on multiple servers.
If you’ve used the SimpleXML functions in PHP, you may have noticed some strange things happening with CDATA values in your XML file/string. All I needed to do was extract the value of my CDATA fields, however these were always coming back blank in the structure that simplexml_load_file returns.
Finally, after hours of trawling google, [...]
I’ve been looking for a simple way to convert XML into an array in PHP, like the Perl library XML::Simple does. The best bet with PHP is SimpleXML, which provides a pretty simple interface for parsing XML in PHP.
Take a simple example XML file:
<list type=’randomlist’>
<item>
<name>Item [...]
I like to display dates relative to now, aka Facebook. So instead of displaying a date in the form 3-2-2008 (which can get confusing if you are in the US/Europe etc with dates being switched) I like to show it as 2 weeks ago as this is far easier to read.
So I came accross the [...]
Download the RPMs you require for your new PHP installation from the following URLs. If you have RPMs that aren’t listed, use the search function:
php-5.2.5
pcre-6.6
php-cli-5.2.5
php-common-5.2.5
php-pear-1.7.1
php-gd-5.2.5
php-pd-5.2.5
php-mysql-5.2.5
sqllite
Or if you are upgrading another distrobution, search for the equivalents here.
Check which PHP RPMs you currently have installed:
rpm -qa | grep phpphp-5.1.6-1.6
Output should be something like:
php-pear-1.4.9-1.2
php-pdo-5.1.6-1.6
php-gd-5.1.6-1.6
php-mysql-5.1.6-1.6
** Do The Following [...]
I have had a requirement to encrypt in Perl and decrypt in Php. As a result, I’ve been looking at ways to get this to work. There are a number of people who have managed to get this working using the Crypt::Blowfish module in perl and mcrypt_cbc function in Php.
That’s great unless you [...]
Recently I was creating a new website from scratch, which required authentication and the use of PHP’s sessions functionality. I used the session to store information which I needed to be persistent between pages:
Page 1
Page 2
However page 2 would never display the user ID value. I couldn’t work out what was causing the [...]
I am always forgetting the syntax for foreach() loops in javascript. They actually don’t exist (not in their Perl or Php form anyway) and they differ in that they iterate over all the properties of an object, not an array. You can get some unexpected results if you aren’t careful.
In perl you do [...]
I have been tearing my hair out trying to work out why PHP’s json_decode() function wouldn’t convert a JSON string sent via AJAX using the prototype.js .toJSON() function. I was sending the string:
{\”Assembly_ID\”: 1}
However json_decode() in php would return NULL. After some fiddling around, I determined that json_decode() doesn’t like the [...]
Along the same lines of my previous post, I was looking for a PHP equivalent to Data::Dumper for Perl. Php has a builtin function called var_dump() which will give you some fairly ugly output about your array/hash/object, however I was looking for something a little more elegant.Thankfully the folks over at Pear have come up [...]