Wednesday 18 December 2013

PHP Get a part of an array

Extract a slice of the array
Get a part of an array


<?php
$input 
= array("a""b""c""d""e");
$output array_slice($input2);      // returns "c", "d", and "e"$output array_slice($input, -21);  // returns "d"$output array_slice($input03);   // returns "a", "b", and "c"

// note the differences in the array keys
print_r(array_slice($input2, -1));print_r(array_slice($input2, -1true));?>

array_slice — Extract a slice of the array

Description ¶

array array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_keys= false ]] )
array_slice() returns the sequence of elements from the array array as specified by the offset and lengthparameters.

Parameters ¶

array
The input array.
offset
If offset is non-negative, the sequence will start at that offset in the array. If offset is negative, the sequence will start that far from the end of the array.
length
If length is given and is positive, then the sequence will have up to that many elements in it. If the array is shorter than the length, then only the available array elements will be present. If length is given and is negative then the sequence will stop that many elements from the end of the array. If it is omitted, then the sequence will have everything from offset up until the end of the array.
preserve_keys
Note that array_slice() will reorder and reset the numeric array indices by default. You can change this behaviour by setting preserve_keys to TRUE.

Return Values ¶

Returns the slice.

PHP sort a multi dimensional array with values


array_multisort — Sort multiple or multi-dimensional arrays

We have an array of rows, but array_multisort() requires an array of columns, so we use the below code to obtain the columns, then perform the sorting.

$resultData = array(0=>array('name'=>'test1', 'email'=> 'test1@test.com', 'phone'=>990000001),
                               1=>array('name'=>'test2', 'email'=> 'test2@test.com', 'phone'=>990000002),  
                               2=>array('name'=>'test3', 'email'=> 'test3@test.com', 'phone'=>990000003),
foreach ($resultData as $key => $row) {
     $mid[$key]  = $row['name'];
}
array_multisort($mid, SORT_DESC, $resultData);
 

Friday 6 December 2013

E-Mail Attachments With PHP's mail() Function

Below is a full working example of how to include one or more attachments to an outbound e-mail utilizing PHP's mail() function. Usage Example


<?php
$to = 'test@example.com';
$from = 'source@example.com';
$subject = 'See Attachments';
$message = 'Please review the following attachments.';

// Define a list of FILES to send along with the e-mail. Key = File to be sent. Value = Name of file as seen in the e-mail.
$attachments = array(
 '/tmp/WEDFRTS' => 'first-attachment.png',
 '/tmp/some-other-file' => 'second-attachment.png'
);

// Define any additional headers you may want to include
$headers = array(
 'Reply-to' => 'source@example.com',
 'Some-Other-Header-Name' => 'Header Value'
);

$status = mailAttachments($to, $from, $subject, $message, $attachments, $headers);
if($status === True) {
 print 'Successfully mailed!';
} else {
 print 'Unable to send e-mail.';
}


<?php
function mailAttachments($to, $from, $subject, $message, $attachments = array(), $headers = array(), $additional_parameters = '') {
 $headers['From'] = $from;

 // Define the boundray we're going to use to separate our data with.
 $mime_boundary = '==MIME_BOUNDARY_' . md5(time());

 // Define attachment-specific headers
 $headers['MIME-Version'] = '1.0';
 $headers['Content-Type'] = 'multipart/mixed; boundary="' . $mime_boundary . '"';

 // Convert the array of header data into a single string.
 $headers_string = '';
 foreach($headers as $header_name => $header_value) {
  if(!empty($headers_string)) {
   $headers_string .= "\r\n";
  }
  $headers_string .= $header_name . ': ' . $header_value;
 }

 // Message Body
 $message_string  = '--' . $mime_boundary;
 $message_string .= "\r\n";
 $message_string .= 'Content-Type: text/plain; charset="iso-8859-1"';
 $message_string .= "\r\n";
 $message_string .= 'Content-Transfer-Encoding: 7bit';
 $message_string .= "\r\n";
 $message_string .= "\r\n";
 $message_string .= $message;
 $message_string .= "\r\n";
 $message_string .= "\r\n";

 // Add attachments to message body
 foreach($attachments as $local_filename => $attachment_filename) {
  if(is_file($local_filename)) {
   $message_string .= '--' . $mime_boundary;
   $message_string .= "\r\n";
   $message_string .= 'Content-Type: application/octet-stream; name="' . $attachment_filename . '"';
   $message_string .= "\r\n";
   $message_string .= 'Content-Description: ' . $attachment_filename;
   $message_string .= "\r\n";

   $fp = @fopen($local_filename, 'rb'); // Create pointer to file
   $file_size = filesize($local_filename); // Read size of file
   $data = @fread($fp, $file_size); // Read file contents
   $data = chunk_split(base64_encode($data)); // Encode file contents for plain text sending

   $message_string .= 'Content-Disposition: attachment; filename="' . $attachment_filename . '"; size=' . $file_size.  ';';
   $message_string .= "\r\n";
   $message_string .= 'Content-Transfer-Encoding: base64';
   $message_string .= "\r\n\r\n";
   $message_string .= $data;
   $message_string .= "\r\n\r\n";
  }
 }

 // Signal end of message
 $message_string .= '--' . $mime_boundary . '--';

 // Send the e-mail.
 return mail($to, $subject, $message_string, $headers_string, $additional_parameters);
}

Monday 4 November 2013

PHP Date difference between two dates

$dStart = new DateTime('2013-11-26');
 $dEnd  = new DateTime(date('Y-m-d'));
 $dDiff = $dStart->diff($dEnd);
//Date difference in days
  $date_diff_days    =     $dDiff->days;
             
    Another Method
        
$now = time();    
$your_date = strtotime('2013-11-26');
$datediff = $now - $your_date;
$date_diff_daysfloor($datediff/(60*60*24));