Okay, so I put all my IP ranges in the database and wrote a function which takes an IP as input and checks the database to see if the IP is in the listed ranges. It returns TRUE if the IP is not in any of the ranges. The table where I put the ranges looks like:
Performing data/time calculation is basic requirement in any programming project. No serious application can hide from it, so thankfully PHP itself comes with some very useful data/time functions. But, I feel that those PHP date/time functions are probably great for data/time display and formatting, but not so much for calculating for things like the beginning/end of the week, the number of seconds between two dates etc.
Again thankfully, after a visit to phpclasses.org, I found a nifty little class written by Steve Powell (way back in 2004) called DateClass which did what I wanted. Unfortunately, the documentation was only a class reference sheet, and doesn’t come with any examples to quickly get things going.
The DateClass package actually contains 2 individual classes. One for manipulating dates and the other for manipulating date spans. This post will only cover the actual DateClass object. I’ll post the followup in Part 2 for the DateSpanClass object soon.
Let’s get cracking.
Example 1: Display the current date and time.
<?php
// include the DateClass class
include('dateclass.php');
// initiate the class
$dc = new DateClass();
// output to string
echo $dc->ToString();
?>
Yes, I know, this does the same as calling date()function in PHP.
echo date('Y-m-d H:i:s');
So, why should you use such a long winded method? Well, it’s the other methods in the DateClass class that we’re interested in.
The class comes with the standard calls you’ll find equivalent from PHP, such as, Year(), Month(), Day(), Hours(), Minutes(), Seconds(), Timestamp(), etc. So they’re all pretty self explanatory. Instead, let’s look at some of the more interesting methods.
Example 2: Find the beginning and end of the week.
<?php
// include the DateClass class
include('dateclass.php');
// initiate the class
$dc = new DateClass();
// calling BOW() will return another DateClass object
// for beginning of the week.
$bow = $dc->BOW();
// output the datetime for the beginning of this week.
echo $bow->ToString();
// calling EOW() will return me another DateClass object
// for end of the week
$eow = $dc->EOW();
// output the datetime
echo $eow->ToString();
// destroy the DateClass objects
$dc = "";
$bow = "";
$eow = "";
?>
I think you get the idea. The class also includes methods for:
BOM() and EOM() – Beginning and end of the month
BOQ() and EOQ() – Beginning and end of the quarter
BOY() and EOY() – Beginning and end of the year
Quarter() – which returns the calendar quarter (1-4) the current date value of class is in.
Example 3: Calculating dates 2 days forward and then 2 months back
Lastly, let’s look at the date calculation using the Add() method.
<?php
include ('dateclass.php');
// initiate the class
$dc = new DateClass();
// add 2 days to the current date
$nd = $dc->Add('days',2);
// subtract 2 months to the previous calculated date
$nd = $dc->Add('months',-2);
// output the new datetime
echo $nd->ToString();
?>
Although using DateClass is a bit more verbose than say, using strtotime() approach, I find this more intuitive and easier to understand. But that’s just me.
Coming up in my next post, I’ll cover what the DateSpanClass can do, and how to return results like the number of whatever (days, months, hours, even weekdays) between 2 dates. Watch out for it.
source :php classs
strpos function to check character in string using php
suppose i would like to find : in string so chk this out
$username = ’mrphpguru:aphp coder’;
$pos = strpos($username, ”!”);
if ($pos == -1)
{
print Please ‘don’t put : in your usernames’;
}
else
{
print ’No special character in the string.Thanks’;
}
So using strpos function we can find a character in a string using php function strpos.
I am discussion how to use cURL function of php to download a web page and save it .
A typical PHP cURL usage follows the following sequence of steps.
curl_init – Initializes the session and returns a cURL handle which can be passed to other cURL functions.
curl_opt – This is the main work horse of cURL library. This function is called multiple times and specifies what we want the cURL library to do.
curl_exec – Executes a cURL session.
curl_close – Closes the current cURL session.
Below are some examples which should make the working of cURL more clearer.
The below PHP code uses cURL to download Google’s RSS feed.
<?php
/**
* Initialize the cURL session
*/
$ch = curl_init();
/**
* Set the URL of the page or file to download.
*/
curl_setopt($ch, CURLOPT_URL,
'http://news.google.com/news?hl=en&topic=t&output=rss');
/**
* Create a new file
*/
$fp = fopen('rss.xml', 'w');
/**
* Ask cURL to write the contents to a file
*/
curl_setopt($ch, CURLOPT_FILE, $fp);
/**
* Execute the cURL session
*/
curl_exec ($ch);
/**
* Close cURL session and file
*/
curl_close ($ch);
fclose($fp);
?>
can anyone help with this please, im using this linked to a textbox so that people can enter info and it will save to a database whilst when viewed outside of a textbox it will show normally in html. the problem is that if someone tries to edit there entry then it shows with ( ) on the end of the lines. this is kind of a problem because the people using my form wont have a clue what the hell is going on. is there a way to remove this from the textbox?
second problem with it. if you then resubmit the info from the textbox then it duplicates the ( ) and causes there to be 2 trailing ‘s on the end of each line…