This simple function formats a Unix Timestamp as one ready to be inserted into MySQL.
function mktimestamp($epoch) {
$epoch = date("Y-d-m H:i:s", $epoch);
return $epoch;
}
Posted at 11:17 on 2 st July 2009
This is the correct formatting to use when using the date() function when building MySQL TIMESTAMPS.
date("Y-d-m H:i:s");
Example: 2009-21-07 20:58:15
Other than that it’s pretty simple. Unfortunately, MySQL doesn’t support Unix Timestamps, which would mean that it’s a good idea to have a function to deal with this.
Posted at 8:00 on 2 st July 2009
This is a basic OPML sample to base OPML building scripts on top of. Pulled from a nearly blank OmniOutliner OPML export. The fields “text”, “Date”, “URL” can be changed to how you desire.
<?xml version="1.0" encoding="UTF-8"?>
<opml version="1.0">
<head>
<title>example</title>
<expansionState></expansionState>
</head>
<body>
<outline text="My Title" URL="My URL" Date="It's Date"/>
</body>
</opml>
Posted at 12:25 on 7 th July 2009
This code snippet mini-project, returns a geocode (that is, Latitude and Longitude) from a given address.
Note: This requires that you obtain your own Google Maps API Key and on it’s own violates the Google Maps API Terms and Conditions. In short, you must accompany this with a map, then you’re safe.
It features some basic error handling, which will catch the status messages returned. Unfortunately, this makes the code a little messy, but otherwise you’d have nothing returned.
$key = "your-key";
/* Given data */
$street = "My Street";
$town = "My Town";
$country = "My Country";
$postcode = "My Postcode";
$address = urlencode($street . ", " . $town . ", " . $country);
$xml_input = "http://maps.google.com/maps/geo?q={$address}&output=xml&oe=utf8&sensor=false&key={$key}";
$xml = simplexml_load_file($xml_input);
$status = $xml->Response->Status->code; // returned status code
if ($status == "500") {
echo "Request could not be completed due to a server error. Please try again.";
}
if ($status == "601") {
echo "You didn't pass an address. Please try again.";
}
if ($status == "602") {
echo "The address could not be found. Please try again.";
}
if ($status == "603") {
echo "The address cannot be returned due to legal or contractual reasons.";
}
if ($status == "610") {
echo "The API key used is invalid or doesn't match the domain.";
}
if ($status == "620") {
echo "Too many requests have been sent. You've either reached the limit, or sent too many at once.";
}
else {
echo "<h2>Coordinates";
$coords = $xml->Response->Placemark->Point->coordinates; // return coords
// splits and reverses the returned string to make it correct
$latlong = explode(",", $coords);
echo $latlong[1] . ", " . $latlong[0];
}
This returns the latitude and longitude of the address passed to it, for example: 51.2343500, -0.7325510. You can also access this through the $latlong array.
Naturally, it’s had my details stripped out of it. You can get an API key here.
Read more: http://code.google.com/apis/maps/documentation/geocoding/index.html
Posted at 9:44 on 5 th July 2009
This is the first in a series of posts which will provide examples of the various Microformats. “Geo” is used to markup Latitude and Longitude data into web pages, rather useful for providing location data near/around maps.
<div class="geo">
<abbr class="latitude" title="37.408183">N 37° 24.491
<abbr class="longitude" title="-122.13855">W 122° 08.313
</div>
Source: Microformats Wiki: Geo
Posted at 9:05 on 4 th July 2009
This is an extension of the Gravatar docs, which seemed to be a little lackluster in the PHP department.
$email = "nickcharlton91@gmail.com";
$size = "80";
$gravatar = "http://www.gravatar.com/avatar/" . md5($email) . "?s={$size}";
<img src="<?php echo $gravatar ?>" alt="Gravatar" width="<?php echo $size ?>" height="<?php echo $size ?>" />
I’m sure there are similar ways to do this, some most likely more flexible, but this is a great way to slot in user icons in projects. I especially like Github’s way of not making you upload a photo. (Obviously, it’s biggest use is in Blogs, but I digress.)
Posted at 5:26 on 4 th July 2009
Whilst this is not a perfect way to protect against attacks, it is a step.
function clean_input($input) {
// note, needs a MySQL connection
$clean = strip_tags($input);
$cleaner = mysql_real_escape_string($clean);
return $cleaner;
}
Best used wrapped around a string. e.g. clean_input($string);
Posted at 3:01 on 2 th July 2009
This function validates an email address passed to it and returns either True or False.
function validate_email($email) {
$match = "/^[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z_+])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9}$/";
$regex = preg_match($match, $email);
return $regex;
}
Posted at 6:52 on 5 rd July 2009
This simple snippet redirects from one page to another. Note: Must be sent before the rest of the content.
header('Location: login.php');
exit();
Posted at 2:57 on 3 st July 2009
This is an improvement over this post. This also produces an uptime, but properly splits the seconds in “days”, “hours”, “minutes” & “seconds”, as well as appending s or not if it should be there.
Could further be improved to include options to be called from the function.
function uptime() {
$procuptime = popen("cat /proc/uptime | awk -F \" \" '{print $1}'", "r");
$read = fread($procuptime, 2096);
$day = floor($read / 86400);
$hour = floor(($read % 86400) / 3600);
$minute = floor((($read % 86400) % 3600) / 60);
$second = floor(((($read % 86400) % 3600) % 60));
if ($day == "1") {
echo "$day day ";
} else {
echo "$day days ";
}
if ($hour == "1") {
echo "$hour hour ";
} else {
echo "$hour hours ";
}
if ($minute == "1") {
echo "$minute minute ";
} else {
echo "$minute minutes ";
}
if ($second == "1") {
echo "$second second ";
} else {
echo "$second seconds ";
}
}
Posted at 1:35 on 1 nd June 2009