This could come in handy if you have a table with multiple rows and if you wish to alternate between 2 colors. We’ll need to use the Modulus Arithmetic Operator for this (%). Modulus (%) gets the remainder after a division has been made between 2 numbers. eg. 3%2 = 1 Code Example: <table border="0" cellpadding="2" cellspacing="4"> <?php $rows = 10; //Number of rows ...
How to convert a MySQL Datetime field to a Unix Timestamp using PHP
In the example you’ll find a handy PHP function that will do the conversion. <?php $DATETIME = '2010-04-22 19:54:38'; echo convertDateTime($DATETIME); /* * This function converts a mysql datetime to a unix timestamp * The format should be "YYYY-MM-DD HH:MM:SS" * * @param $datetime string Contains the DATETIME value * @return UNIX TIMESTAMP */ function...
How to get the current UNIX timestamp using PHP
UNIX TIMESTAMP: Is the time measured in seconds since the start of the Unix epoch “1 January 1970”
Use the PHP time function as per example below
echo time(); //Will print out the current timestamp
//OUTPUT: 1285365600
To read more about the time function click here
How to get the first element of an array using PHP
If you have the array
$array = array('a','b','c','d');
you will need to get the value a from the array
Answer:
Use the array_shift function available in PHP 4 and 5 which shifts an element off the beginning of an array. See example below
$array = array('a','b','c','d');
$first_element = array_shift($array);
echo $first_element;
The above code will output a
How to change the Return-path email address using the PHPMailer class
Fill the $Sender property with the email address you wish to have as the Return-path E.g. $mail->Sender="[email protected]"; This property can be found on Line 97 in the class.phpmailer.php file /** * Sets the Sender email (Return-Path) of the message. If not empty, * will be sent via -f to sendmail or as 'MAIL FROM' in smtp mode. * @var string */ public $Sender =...
Getting PHP Fatal error: Can’t use function return value in write context
Make sure that you did not include a function within the empty PHP function. eg
if(empty(getUsers())){}
How to validate a date format using PHP
To check whether the date entered is in the correct format NOTE: We will use the following date format within this example YYYY-MM-DD Answer: Use the preg_match ‘Perform a regular expression match’ PHP function available for PHP 4 & 5 Find the correct expression And use the following code to check for the date format if(preg_match('/[12]\d{3}-[01]\d-[0123]\d/',$date)){ $msg...
How to compare two dates using PHP
First we need to convert the two dates to UNIX Timestamps using the PHP function strtotime Then we can compare the two dates in anyway we wish, in the example below I’m checking that the first date is older than the second date $date1 = '1999-10-11'; $date2 = '2010-10-11'; $firstDate = strtotime($date1); $secondDate = strtotime($date2); if($firstDate < $secondDate){ echo 'First...
How to add months to a given date using PHP
By using the PHP functions date and strtotime this can be accomplished. Example $date = '2010-03-22'; $new_date = date('Y-m-d',strtotime('+4 months',strtotime($date))); echo $new_date; //Displays 2010-07-22 Explanation of PHP functions: 1) FUNCTION date DESCRIPTION string date ( string $format [, int $timestamp ] ) PARAMETERS USED IN THIS EXAMPLE Y = A full numeric representation of a year...
How to check if an array contains a empty element or value
Use the following code example
$array = array('a','b',''); //The array to check for empty values
echo (in_array(' ', $array)) ? 'Empty values exists' :'No empty values'; ?> //Checking if empty values exist in the array
NOTE:
in_array = searches a array for a specific value
What is the difference between public, private and protected keywords?
Public – Are variables that are visible to all classes
Private -Are variables that are visible only to the class they belong
Protected – Are variable that are visible to the class they belong and to the subclasses of the class they belong
How to check for empty values within a array using PHP
If you have an array called $array with the below values Array ( [0] => 1299016800 [1] => [2] => ) As we can see in the above example we have 2 empty values within the array, now how do we determine this using PHP First we get the size of the array $mainSize = sizeof($array); Now we get the size of the array without the empty values $emptySize =sizeof(array_filter($array)); Now...
How to transform a month number to the relevant month name using PHP
This can be obtained by combining the date function with the mktime function For example if I want to get Jan from the number 1 which represents the first monthin the year I would use something similar to the below $i = 1; //The number 1 represents January echo date("M",mktime(0,0,0,$i,1,2010)); //This will output Jan So if you want to output Feb the value of $i would be 2 etc USAGE: date =...
How to raise the PHP memory limit, upload limit, or script execution time limit within the .htaccess file
Open your .htaccess file and add the line below to it
php_value memory_limit 40M
This will increase the PHP memory limit, if the altering of the .htaccess file is allowed
How to auto submit a Sitemap to Google using PHP
This can be done via a ping service from Google Webmaster Tools Add the following PHP function function submitSitemap($site) { $url = ''.htmlentities($site.'/sitemap.xml'); $response = file_get_contents($url); if($response){ echo $response; }else{ echo "Failed to submit sitemap"; } } Now you need to...
How to detect the data type of a PHP variable
The data type of a PHP variable can be detected by using the built-in PHP functiongettype() (PHP 4, PHP 5)
Example:
$var ="This is a sentence"
echo $var; //This will return "string"
The following possible values can be returned:
boolean
integer
double
string
array
object
resource
NULL
unknown type
Calculating the difference in days between 2 dates in PHP
$startDate = '1999-03-12'; $endDate = '2011-03-12'; $days = (strtotime($endDate)-strtotime($startDate)) / (60 * 60 * 24); echo $days; //Will output 4383 In the above example we have a startdate and a enddate. We need to find out how many days are between the startdate and the enddate. We convert both dates to a UNIX Timestamp using the strtotime PHP function. After...
How to remove all characters exept numbers from a string using PHP
This is really easy, we will make use of the preg_replace PHP Function which is 100% compatible with PHP 5.3 Example: $string = 'This is my phone number: +28 (0)212 1234!!! ' $numberString = preg_replace('[\D]', '', $string); echo $numberString; //Will output 2802121234 This is a good way to format phone numbers etc. The regular expression character \D means to match a character that’s...
How to remove all empty/null values in an array using PHP
If you have an array with empty values and you wish to remove the empty valuesyou can use the following one line code
$array = array('','1','','3');
$array = array_filter($array, 'strlen');
echo '<pre>';
print_r($array);
echo '</pre>';
The result will be only the NOT NULL values
Array
(
[1] => 1
[3] => 3
)