Showing posts with label Php Lessons. Show all posts
Showing posts with label Php Lessons. Show all posts
Php Custom function - Difference between two dates,time and date
The function presented in this page, CalculateDiff(), can be used to get the difference between 2 dates,
time and date .
This function takes two parameters: the date1 and date2. It can be used various date / time formats: Unix Timestamp, or a string
Function that returns the difference between two date-time Returns an array containing a string with a textual reprezentation of the difference, and separately: the days, hours, minutes, seconds, total hours, total minutes, and total seconds.
array (
'diff' => '20 minutes 28 seconds ',
'days' => 0, 'hours' => 0, 'min' => 20, 'sec' => 28,
'totalhours' => 0, 'totalmin' => 20, 'totalsec' => 1228
)
array (
'diff' => '61 days 16 hours 9 minutes 20 seconds ',
'days' => 72, 'hours' => 16, 'min' => 9, 'sec' => 20,
'totalhours' => 1744, 'totalmin' => 104649, 'totalsec' => 6278960
)
array (
'diff' => '23 days 18 hours 15 minutes 0 seconds ',
'days' => 23, 'hours' => 18, 'min' => 15, 'sec' => 0,
'totalhours' => 570, 'totalmin' => 34215, 'totalsec' => 2052900
)
array (
'diff' => '4 hours 43 minutes 11 seconds ',
'days' => 0, 'hours' => 4, 'min' => 43, 'sec' => 11,
'totalhours' => 4, 'totalmin' => 283, 'totalsec' => 16991
)
This function takes two parameters: the date1 and date2. It can be used various date / time formats: Unix Timestamp, or a string
Function that returns the difference between two date-time Returns an array containing a string with a textual reprezentation of the difference, and separately: the days, hours, minutes, seconds, total hours, total minutes, and total seconds.
<? function CalculateDiff($date1, $date2) { // sets to use $date1 and $date2 as Unix Timestamp if (!is_int($date1)) $date1 = strtotime($date1); if (!is_int($date2)) $date2 = strtotime($date2); // if the difference is negative, the hours are from different days, and adds 1 day (in sec.) $diff = ($date2 >= $date1) ? $date2 - $date1 : 86400 + $date2 - $date1; // define the number of days, hours, minutes and seconds in difference $d = floor($diff / 86400); $h = floor(abs($diff - $d * 86400) / 3600); $m = floor(abs($diff - $d * 86400 - $h * 3600) / 60); $s = $diff % 60; // sets the words, singular or plural $dstr = ($d == 1) ? ' day ' : ' days '; $hstr = ($h == 1) ? ' hour ' : ' hours '; $mstr = ($m == 1) ? ' minute ' : ' minutes '; $sstr = ($s == 1) ? ' second ' : ' seconds '; // setings for the string added in textual reprezentation of the difference $sdiff_d = ($d != 0) ? $d . $dstr : ''; $sdiff_h = ($h != 0) ? $h . $hstr : ''; $sdiff_m = ($m != 0) ? $m . $mstr : ''; return array( 'diff' => $sdiff_d . $sdiff_h . $sdiff_m . $s . $sstr, 'days' => $d, 'hours' => $h, 'min' => $m, 'sec' => $s, 'totalhours' => floor($diff / 3600), 'totalmin' => floor($diff / 60), 'totalsec' => $diff ); } ?>
Examples of CalculateDiff() function usage, with different date-time formats:
<?php // difference between 2 times (in hours:min:sec) $rs1 = CalculateDiff('8:35:6', '8:55:34'); var_export($rs1); ?>Result:
array (
'diff' => '20 minutes 28 seconds ',
'days' => 0, 'hours' => 0, 'min' => 20, 'sec' => 28,
'totalhours' => 0, 'totalmin' => 20, 'totalsec' => 1228
)
<?php // difference between a previous date-time and now $rs2 = CalculateDiff('07/19/2012 14:10:00', 'now'); var_export($rs2); ?>Result:
array (
'diff' => '61 days 16 hours 9 minutes 20 seconds ',
'days' => 72, 'hours' => 16, 'min' => 9, 'sec' => 20,
'totalhours' => 1744, 'totalmin' => 104649, 'totalsec' => 6278960
)
<?php // difference between 2 date-times $rs3 = CalculateDiff('25 August 2012 14:10:00', '18-09-2012 08:25:00'); var_export($rs3); ?>Result:
array (
'diff' => '23 days 18 hours 15 minutes 0 seconds ',
'days' => 23, 'hours' => 18, 'min' => 15, 'sec' => 0,
'totalhours' => 570, 'totalmin' => 34215, 'totalsec' => 2052900
)
<?php // difference between 2 date-time, with Timestamp $rs4 = CalculateDiff(1348012438, 1348029429); var_export($rs4); ?>Result:
array (
'diff' => '4 hours 43 minutes 11 seconds ',
'days' => 0, 'hours' => 4, 'min' => 43, 'sec' => 11,
'totalhours' => 4, 'totalmin' => 283, 'totalsec' => 16991
)
Labels:
Php Lessons
Install Zend Framework on Ubuntu Linux
Zend Framework on Ubuntu Linux
Labels:
Installation Php,
Php Lessons
Install Zend Framework in Mac OS
Installing zend Framework in Mac Os
Labels:
Installation Php,
Php Lessons
Mysql Full-Text Search
The Full text Search will produce results faster than Simply using “OR” in sql query.
After we have our tables on MyISAM storage engine we just need to add full text index to our selected columns on the desired table.Which is again can be done using PHPmy admin or Use the Query below to add Full Text INDEX To our Tables.The code Below will add Full text index to title and Description.columns
or using phpmyadmin as below.
The above query will match two columns title and description for Words “Sujal Shah boy” .For that to happen we need to have a multiple column Full text index, which i just told to how to create above.
As we just seen the Query “Sujal Shah boy” There is no Operator in between .We could use operators for further refinement or as per needs, only if we use IN BOOLEAN MODE modifier. In Query “Sujal Shah boy” having no operator means Simply “OR” in boolean mode full text search.The Full text Search will Use OR in boolean mode .which means Will find all the rows having any one of those words.
+ means AND
- means NOT
[no operator] means OR
Here i have few examples using Mysql Full text search with operators:
Mysql Full-Text Search Basics
For start using mysql full text search for multi-word queries we need to understand its basics.Firstly it can’t be implemented on Mysql innoDB storage engine tables,So the First thing we must do is convert our innoDB table to MyISam Engine,which is quite easy can be done using phpmyadmin under the operations tab .After we have our tables on MyISAM storage engine we just need to add full text index to our selected columns on the desired table.Which is again can be done using PHPmy admin or Use the Query below to add Full Text INDEX To our Tables.The code Below will add Full text index to title and Description.columns
Add Full text Index to Table columns
ALTER TABLE inventory ADD FULLTEXT(title, description)
Mysql Full Text Index on Single Column:
Mysql Full Text Index on Multiple Column:
Note:The FULL text index on single column and pair of column is differet things.
Usage MySql Full Text Search for Multiword String:
So Now Our table Are ready for Full text Search as we have provided full index.Now to search for any word say :we wana search “Sujal Shah” then we’ll tyep Query as:
Searching using Full text Index.
SELECT *, MATCH(title, description) AGAINST ('Sujal Shah boy') AS artist FROM artist_listing ORDER BY artist DESC
The above query will match two columns title and description for Words “Sujal Shah boy” .For that to happen we need to have a multiple column Full text index, which i just told to how to create above.
BOOLEAN MODE Operators in MySQL Full Text Search :
If We Use BOOLEAN FULL TEXT SEARCHE which means we’ll add boolean mode and we will have some useful operators available to us if we use Boolean mode.As we just seen the Query “Sujal Shah boy” There is no Operator in between .We could use operators for further refinement or as per needs, only if we use IN BOOLEAN MODE modifier. In Query “Sujal Shah boy” having no operator means Simply “OR” in boolean mode full text search.The Full text Search will Use OR in boolean mode .which means Will find all the rows having any one of those words.
Example
SELECT *, MATCH(title, description) AGAINST ('+Sujal -Shah +boy' IN BOOLEAN MODE) AS artist FROM artist_listing ORDER BY artist DESC
- means NOT
[no operator] means OR
Here i have few examples using Mysql Full text search with operators:
- +Sujal +Shah +boy:Will find only rows having all three words
- +Sujal -Shah : will find rows that contain Sujal but not Shah
- +Sujal Shah :will find rows that must contain Sujal, but rank rows higher if they also contain “Shah”.
- +Sujal ~Shah :Fetch rows that must have the word “Sujal”, but if the row also have the word “Shah”, rate it lower than if row does not.
- ‘+Sujal +(>Shah <boy)’ :Get the rows that contain the words “Sujal” and “Shah”, or “boy” and “boy” (in any order), but rank “Sujal Shah” high than “Sujal boy”.
MySQL FULL TEXT SEARCH LIMITS :
- Full text search can Only be used with MyIsam TABLES.
- For Full text Search to to work, there must be Full text indexes defined otherwise SQL will throw error and Full text search will not work.
Labels:
Php Lessons
Php Date difference - Php Datetime object
Example : add and diff method of Datetime object
Here in below example DateInterval object has the argument like P01Y01M10DHere the 01Y indicates 1 year
Here the 01M indicates 1 month
Here the 10D indicates 10 days
<?php $givendate='2001-01-01'; //add one year one month and ten days to given date $date = new DateTime($givendate); $date->add(new DateInterval('P01Y01M10D')); echo $date->format('Y-m-d') . "\n"; //remove one year one month and ten days to given date $date = new DateTime($givendate); $date->diff(new DateInterval('P01Y01M10D')); echo $date->format('Y-m-d') . "\n"; ?>Output:
2002-02-11
2001-01-01
Example : Calculate Age of a Person using Datetime object
<?php //Php Date difference - Php Datetime object $birthday="1984-10-18"; //creating object for birthday $birthday = new DateTime($birthday); //createing object for current date $currentdate = new DateTime(); //calculate age using php Datetime object method $age=$currentdate->diff($birthday); print_r($age); ?>Output:
DateInterval Object ( [y] => 27
[m] => 10
[d] => 27
[h] => 12
[i] => 38
[s] => 43
[invert] => 1
[days] => 10193
)
Labels:
Php Lessons
Php PDO: Prepare Statement with Insert and Update
Insert data in books table using Php PDO.
The example below will insert title "Php Pamplates" and author "Kishor" in books table.
Now, Update data in books table using Php PDO.
The example below will update title to "Php Books" and author to "Hiren" in books table where id
is 1.
The example below will insert title "Php Pamplates" and author "Kishor" in books table.
Example: Insert query with PDO prepare statement
<?php // configuration variables $dbhost = "localhost"; $dbname = "mydatabase"; $dbuser = "root"; $dbpass = ""; // database connection $connection = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass); // new data $title = 'Php Pamplates'; $author = 'Kishor'; $sql = "INSERT INTO books (title,author) VALUES (:title,:author)"; $query = $connection->prepare($sql); $query->execute(array(':author'=>$author,':title'=>$title)); ?>
Now, Update data in books table using Php PDO.
The example below will update title to "Php Books" and author to "Hiren" in books table where id
is 1.
Example: Update query with PDO prepare statement
<?php // configuration variables $dbhost = "localhost"; $dbname = "mydatabase"; $dbuser = "root"; $dbpass = ""; // database connection $connection = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass); // data to be updated in id 1 $title = 'Php Books'; $author = 'Hiren'; $id = 1; // query $sql = "UPDATE books SET title=?, author=? WHERE id=?"; $query = $connection->prepare($sql); $query->execute(array($title,$author,$id)); ?>
Labels:
Php Lessons
Sending email with multiple attachments using PHP Script
Using mail() function we can send simple mail see simple mail() script.
We can also send multiple attachement using php code and using php mail() function
For this we have to use MIME extenstion
The example below shows how to send multiple attachements with mail.
We have to create one array of files which we want to send in attachement by brosing from interface.
Example:
We can also send multiple attachement using php code and using php mail() function
For this we have to use MIME extenstion
The example below shows how to send multiple attachements with mail.
We have to create one array of files which we want to send in attachement by brosing from interface.
Example:
<?php // array with filenames to be sent as attachment $files = array("file1.jpg","file2.pdf","file3.txt"); // email fields: to, from, subject, and so on $to = "yourmail@mail.com"; $from = "mymail@mail.com"; $subject ="My subject"; $message = "My message"; $headers = "From: $from"; // boundary $semi_rand = md5(time()); $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x"; // headers for attachment $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . " boundary=\"{$mime_boundary}\""; // multipart boundary $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; $message .= "--{$mime_boundary}\n"; // preparing attachments for($x=0;$x<count($files);$x++){ $file = fopen($files[$x],"rb"); $data = fread($file,filesize($files[$x])); fclose($file); $data = chunk_split(base64_encode($data)); $message .= "Content-Type: {\"application/octet-stream\"};\n" . " name=\"$files[$x]\"\n" . "Content-Disposition: attachment;\n" . " filename=\"$files[$x]\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n"; $message .= "--{$mime_boundary}\n"; } // send $success = @mail($to, $subject, $message, $headers); if ($success) { echo "<p>mail sent to $to!</p>"; } else { echo "<p>mail could not be sent!</p>"; } ?>
Labels:
Php Lessons
Subscribe to:
Posts (Atom)