<?php function getAge($dob) { $today = date("Y-m-d"); $diff = date_diff(date_create($dob), date_create($today)); return $diff->format('%yYears, %mMonths, %dDays'); } echo getAge('19-10-1988'); ?> |
Web programming tips and tricks
Wednesday, September 7, 2016
Calculate Age from Date of Birth using PHP
Thursday, August 7, 2014
Difference between print_r and var_dump
The var_dump function displays structured information about variables/expressions including its type andvalue. Arrays are explored recursively with values indented to show structure. It also shows which array values and object properties are references.
The print_r() displays information about a variable in a way that's readable by humans. array values will be presented in a format that shows keys and elements. Similar notation is used for objects.
print_r(null) will return nothing where as var_dump(null) returns NULL which is useful when debugging
Example:
<?php
//$obj = (object) array('qualitypoint', 'technologies', 'India');
$obj =array('qualitypoint', 'technologies', 'India');
//var_dump($obj) will display below output in the screen.
echo "<br>var_dump out put<BR>";
var_dump($obj);
//var_dump(0);
var_dump($obj[0]);
/*
object(stdClass)#1 (3) {
[0]=> string(12) "qualitypoint"
[1]=> string(12) "technologies"
[2]=> string(5) "India"
} */
//And, print_r($obj) will display below output in the screen.
echo "<br>Print_r out put<BR>";
print_r($obj);
print_r($obj[0]);
/*stdClass Object (
[0] => qualitypoint
[1] => technologies
[2] => India
) */
?>
The print_r() displays information about a variable in a way that's readable by humans. array values will be presented in a format that shows keys and elements. Similar notation is used for objects.
print_r(null) will return nothing where as var_dump(null) returns NULL which is useful when debugging
Example:
<?php
//$obj = (object) array('qualitypoint', 'technologies', 'India');
$obj =array('qualitypoint', 'technologies', 'India');
//var_dump($obj) will display below output in the screen.
echo "<br>var_dump out put<BR>";
var_dump($obj);
//var_dump(0);
var_dump($obj[0]);
/*
object(stdClass)#1 (3) {
[0]=> string(12) "qualitypoint"
[1]=> string(12) "technologies"
[2]=> string(5) "India"
} */
//And, print_r($obj) will display below output in the screen.
echo "<br>Print_r out put<BR>";
print_r($obj);
print_r($obj[0]);
/*stdClass Object (
[0] => qualitypoint
[1] => technologies
[2] => India
) */
?>
Tuesday, July 29, 2014
Find second largest value / Salary using Mysql
Using Sub query
Option Query 1
SELECT MAX(salary) FROM emp WHERE salary NOT IN
(SELECT MAX(salary) FROM emp)
Option Query 2
SELECT MAX(salary) FROM emp WHERE salary <(SELECT MAX(salary) FROM emp)
Option Query 3
SELECT MIN (salary) FROM emp WHERE salary in (SELECT TOP 2 (salary) FROM emp ORDER BY salary DESC)
Option Query 1
SELECT MAX(salary) FROM emp WHERE salary NOT IN
(SELECT MAX(salary) FROM emp)
Option Query 2
SELECT MAX(salary) FROM emp WHERE salary <(SELECT MAX(salary) FROM emp)
Option Query 3
SELECT MIN (salary) FROM emp WHERE salary in (SELECT TOP 2 (salary) FROM emp ORDER BY salary DESC)
Monday, July 28, 2014
Difference between parent() vs parents()
Below code clarify some differences between the parent() and parents() methods.
parent(selector) : This returns the one immediate parent of the element. Selector is passed in only required cases, like if we want to check if parent exists for the element or in some other situations. Here is simple example which illustrates the parent method.
parents(selector) : This returns the multiple ancestors. Its not restricted to only one level of parent/ancestors of the element. This is the main difference between the parent and parents method.
For Examples 1
<html>…<body><div class=”one”><div class=”two”><p><span>Some text</span></p></div></div></body></html>
$(‘span’).parent() will select the <p> tag such that the element set in the jQuery object is [span].
$(‘span’).parents() will select all parent tags such that the element set in the jQuery object is [p, div.two, div.one, body, html].
So parent() will select the first parent element while parents() will select all elements straight up the DOM tree.
The .parents() and .parent() methods are similar, except that the latter only travels a single level up the DOM tree. Also, $( "html" ).parent() method returns a set containing document whereas $( "html" ).parents() returns an empty set.
For Examples 2
HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<body>
<div>
<p>
<a href="#" id="anchorTag">Click here</a>
</p>
</div>
</body>
</html>
jQuery For parent()
$('#anchorTag').parent(); //This would return p, immediate parent of a#anchorTag.
jQuery For parents()
$('#anchorTag').parents(); //This would return all the ancestors (p, div, body, html)
//On passing selector it will filter down the matched parent elements by selector
$('#anchorTag').parents('div'); //This would give me only div
closest(selector) : It works like parents(), except that it returns only one parent/ancestor. This is ideal for the situation i mentioned earlier. If i want to check for existence of element in the ancestry tree, then i would prefer to use the closest rather than parents as it only target the selector rather than filtering it from all the ancestor elements. Here is example
$('#anchorTag').closest(); //This would give me empty object as we have not mentioned the selector
$('#anchorTag').closest('div'); //This would give me the div.
parent(selector) : This returns the one immediate parent of the element. Selector is passed in only required cases, like if we want to check if parent exists for the element or in some other situations. Here is simple example which illustrates the parent method.
parents(selector) : This returns the multiple ancestors. Its not restricted to only one level of parent/ancestors of the element. This is the main difference between the parent and parents method.
For Examples 1
<html>…<body><div class=”one”><div class=”two”><p><span>Some text</span></p></div></div></body></html>
$(‘span’).parent() will select the <p> tag such that the element set in the jQuery object is [span].
$(‘span’).parents() will select all parent tags such that the element set in the jQuery object is [p, div.two, div.one, body, html].
So parent() will select the first parent element while parents() will select all elements straight up the DOM tree.
The .parents() and .parent() methods are similar, except that the latter only travels a single level up the DOM tree. Also, $( "html" ).parent() method returns a set containing document whereas $( "html" ).parents() returns an empty set.
For Examples 2
HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
</head>
<body>
<div>
<p>
<a href="#" id="anchorTag">Click here</a>
</p>
</div>
</body>
</html>
jQuery For parent()
$('#anchorTag').parent(); //This would return p, immediate parent of a#anchorTag.
jQuery For parents()
$('#anchorTag').parents(); //This would return all the ancestors (p, div, body, html)
//On passing selector it will filter down the matched parent elements by selector
$('#anchorTag').parents('div'); //This would give me only div
closest(selector) : It works like parents(), except that it returns only one parent/ancestor. This is ideal for the situation i mentioned earlier. If i want to check for existence of element in the ancestry tree, then i would prefer to use the closest rather than parents as it only target the selector rather than filtering it from all the ancestor elements. Here is example
$('#anchorTag').closest(); //This would give me empty object as we have not mentioned the selector
$('#anchorTag').closest('div'); //This would give me the div.
Tuesday, July 22, 2014
Extract XML Data using JavaScript
This code is very useful Extract XML Data using JavaScript.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Extract XML Data using JavaScript </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Extract XML Data using JavaScript</title>
<style>
#books
{
font:13px Arial;
width:590px;
text-align:center;
border:solid 1px #000;
overflow:hidden;
}
#books div
{
width:180px;
text-align:left;
border:solid 1px #000;
margin:1px;
padding:2px 5px;
}
.col1 {float:left;clear:both;}
.col2 {float:left;}
.col4 {float:right;}
</style>
</head>
<body>
<div id="books"></div>
<script type="text/javascript">
var oXHR = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
function reportStatus() {
if (oXHR.readyState == 4) // REQUEST COMPLETED.
showTheList(this.responseXML); // ALL SET. NOW SHOW XML DATA.
}
oXHR.onreadystatechange = reportStatus;
oXHR.open("GET", "lib.xml", true);
// true = ASYNCHRONOUS REQUEST (DESIRABLE), false = SYNCHRONOUS REQUEST.
oXHR.send();
function showTheList(xml) {
var divBooks = document.getElementById('books'); // THE PARENT DIV.
var Book_List = xml.getElementsByTagName('List'); // THE XML TAG NAME.
for (var i = 0; i < Book_List.length; i++) {
// CREATE CHILD DIVS INSIDE THE PARENT DIV.
var divLeft = document.createElement('div');
divLeft.className = 'col1';
divLeft.innerHTML = Book_List[i].getElementsByTagName("BookName")[0].childNodes[0].nodeValue;
var divRight = document.createElement('div');
divRight.className = 'col2';
divRight.innerHTML = Book_List[i].getElementsByTagName("Category")[0].childNodes[0].nodeValue;
var divRightnew = document.createElement('div');
divRightnew.className = 'col4';
divRightnew.innerHTML = Book_List[i].getElementsByTagName("Price")[0].childNodes[0].nodeValue;
// ADD THE CHILD TO THE PARENT DIV.
divBooks.appendChild(divLeft);
divBooks.appendChild(divRight);
divBooks.appendChild(divRightnew);
}
};
</script>
</body>
</html>
After create xml file "lib.xml"
<?xml version="1.0"?>
<!-- Last edited by http://encodedna.com -->
<Library>
<List>
<BookName>Computer Architecture</BookName>
<Category>Computers</Category>
<Price>125.60</Price>
</List>
<List>
<BookName>Advanced Composite Materials</BookName>
<Category>Science</Category>
<Price>172.56</Price>
</List>
<List>
<BookName>Asp.Net 4 Blue Book</BookName>
<Category>Programming</Category>
<Price>56.00</Price>
</List>
<List>
<BookName>Stategies Unplugged</BookName>
<Category>Science</Category>
<Price>99.99</Price>
</List>
<List>
<BookName>Teaching Science</BookName>
<Category>Science</Category>
<Price>164.10</Price>
</List>
<List>
<BookName>Challenging Times</BookName>
<Category>Business</Category>
<Price>150.70</Price>
</List>
<List>
<BookName>Circuit Bending</BookName>
<Category>Science</Category>
<Price>112.00</Price>
</List>
<List>
<BookName>Popular Science</BookName>
<Category>Science</Category>
<Price>210.40</Price>
</List>
<List>
<BookName>ADOBE Premiere</BookName>
<Category>Computers</Category>
<Price>62.20</Price>
</List>
</Library>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Extract XML Data using JavaScript </TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Extract XML Data using JavaScript</title>
<style>
#books
{
font:13px Arial;
width:590px;
text-align:center;
border:solid 1px #000;
overflow:hidden;
}
#books div
{
width:180px;
text-align:left;
border:solid 1px #000;
margin:1px;
padding:2px 5px;
}
.col1 {float:left;clear:both;}
.col2 {float:left;}
.col4 {float:right;}
</style>
</head>
<body>
<div id="books"></div>
<script type="text/javascript">
var oXHR = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
function reportStatus() {
if (oXHR.readyState == 4) // REQUEST COMPLETED.
showTheList(this.responseXML); // ALL SET. NOW SHOW XML DATA.
}
oXHR.onreadystatechange = reportStatus;
oXHR.open("GET", "lib.xml", true);
// true = ASYNCHRONOUS REQUEST (DESIRABLE), false = SYNCHRONOUS REQUEST.
oXHR.send();
function showTheList(xml) {
var divBooks = document.getElementById('books'); // THE PARENT DIV.
var Book_List = xml.getElementsByTagName('List'); // THE XML TAG NAME.
for (var i = 0; i < Book_List.length; i++) {
// CREATE CHILD DIVS INSIDE THE PARENT DIV.
var divLeft = document.createElement('div');
divLeft.className = 'col1';
divLeft.innerHTML = Book_List[i].getElementsByTagName("BookName")[0].childNodes[0].nodeValue;
var divRight = document.createElement('div');
divRight.className = 'col2';
divRight.innerHTML = Book_List[i].getElementsByTagName("Category")[0].childNodes[0].nodeValue;
var divRightnew = document.createElement('div');
divRightnew.className = 'col4';
divRightnew.innerHTML = Book_List[i].getElementsByTagName("Price")[0].childNodes[0].nodeValue;
// ADD THE CHILD TO THE PARENT DIV.
divBooks.appendChild(divLeft);
divBooks.appendChild(divRight);
divBooks.appendChild(divRightnew);
}
};
</script>
</body>
</html>
After create xml file "lib.xml"
<?xml version="1.0"?>
<!-- Last edited by http://encodedna.com -->
<Library>
<List>
<BookName>Computer Architecture</BookName>
<Category>Computers</Category>
<Price>125.60</Price>
</List>
<List>
<BookName>Advanced Composite Materials</BookName>
<Category>Science</Category>
<Price>172.56</Price>
</List>
<List>
<BookName>Asp.Net 4 Blue Book</BookName>
<Category>Programming</Category>
<Price>56.00</Price>
</List>
<List>
<BookName>Stategies Unplugged</BookName>
<Category>Science</Category>
<Price>99.99</Price>
</List>
<List>
<BookName>Teaching Science</BookName>
<Category>Science</Category>
<Price>164.10</Price>
</List>
<List>
<BookName>Challenging Times</BookName>
<Category>Business</Category>
<Price>150.70</Price>
</List>
<List>
<BookName>Circuit Bending</BookName>
<Category>Science</Category>
<Price>112.00</Price>
</List>
<List>
<BookName>Popular Science</BookName>
<Category>Science</Category>
<Price>210.40</Price>
</List>
<List>
<BookName>ADOBE Premiere</BookName>
<Category>Computers</Category>
<Price>62.20</Price>
</List>
</Library>
Wednesday, July 2, 2014
List Of Mysql Queries Used any Programming Language
- mysql_query
- mysql_fetch_array
- mysql_fetch_assocs
- mysql_fetch_row
- mysql_fetch_object
- mysql_close
- mysql_fetch_field
- mysql_fetch_leanth
- mysql_fetch_affected_rows
- mysql_connect
- mysql_pconnect
- mysql_select_db
- mysql_create_db
- mysql_data_seek
- mysql_db_name
- mysql_db_query
- mysql_drop_db
- mysql_error
- mysql_field_flags
- mysql_field_len
- mysql_field_table
- mysql_field_type
- mysql_field_seek
- mysql_free_result
- mysql_insert_id
- mysql_first_dbs
- mysql_list_field
- mysql_ping
- mysql_hum_field
- mysql_read_escape
Sunday, June 15, 2014
HTML5 Video & Audio Player
Video add in webpage (Html) . below code below version html 5.
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="425" height="344" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0">
<param name="allowFullScreen" value="true" />
<param name="allowscriptaccess" value="always" />
<param name="src" value="http://www.youtube.com/v/oHg5SJYRHA0&hl=en&fs=1&" />
<param name="allowfullscreen" value="true" />
<embed type="application/x-shockwave-flash" width="425" height="344" src="http://www.youtube.com/v/oHg5SJYRHA0&hl=en&fs=1&" allowscriptaccess="always" allowfullscreen="true">
</embed>
</object>
Also some above problem , And as you know, you need Flash for this. Or it is often delivered via JavaScript as well so it is not perfect.
The HTML 5 way Nice, clean, minimal code:
<video width="640" height="360" poster="http://video-js.zencoder.com/oceans-clip.png" controls preload>
<source src="http://video-js.zencoder.com/oceans-clip.mp4" type='video/mp4' />
<source src="http://video-js.zencoder.com/oceans-clip.webm" type='video/webm' />
<source src="http://video-js.zencoder.com/oceans-clip.ogv" type='video/ogg' />
</video>
<p> Try this page in a modern browser! Or you can <a href="http://video-js.zencoder.com/oceans-clip.mp4">download the video</a> instead.</p>
Solution
HTML5 Video & Audio Player- no video with support format or mime type found? in firefox
Please Create .htaccess files root folder and put here below code
AddType video/mp4 mp4 m4v
AddType audio/mp4 m4a
AddType video/ogg ogv
AddType audio/ogg ogg oga
AddType video/webm webm
The <video> element has several special attributes that can change or enhance its default behavior.<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="425" height="344" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0">
<param name="allowFullScreen" value="true" />
<param name="allowscriptaccess" value="always" />
<param name="src" value="http://www.youtube.com/v/oHg5SJYRHA0&hl=en&fs=1&" />
<param name="allowfullscreen" value="true" />
<embed type="application/x-shockwave-flash" width="425" height="344" src="http://www.youtube.com/v/oHg5SJYRHA0&hl=en&fs=1&" allowscriptaccess="always" allowfullscreen="true">
</embed>
</object>
Also some above problem , And as you know, you need Flash for this. Or it is often delivered via JavaScript as well so it is not perfect.
The HTML 5 way Nice, clean, minimal code:
<video width="640" height="360" poster="http://video-js.zencoder.com/oceans-clip.png" controls preload>
<source src="http://video-js.zencoder.com/oceans-clip.mp4" type='video/mp4' />
<source src="http://video-js.zencoder.com/oceans-clip.webm" type='video/webm' />
<source src="http://video-js.zencoder.com/oceans-clip.ogv" type='video/ogg' />
</video>
<p> Try this page in a modern browser! Or you can <a href="http://video-js.zencoder.com/oceans-clip.mp4">download the video</a> instead.</p>
Solution
HTML5 Video & Audio Player- no video with support format or mime type found? in firefox
Please Create .htaccess files root folder and put here below code
AddType video/mp4 mp4 m4v
AddType audio/mp4 m4a
AddType video/ogg ogv
AddType audio/ogg ogg oga
AddType video/webm webm
autoplay* | Tells the browser to immediately start downloading the video and play it as soon as it can. Note that mobile browsers generally do not support this attribute, the user must tap the screen to begin video playback. |
preload | Hint to the browser about whether optimistic downloading of the video itself or its metadata is considered worthwhile. Options are:
|
poster | Provides an image to show before the video loads |
controls* | Shows the default video controls (play, pause, etc) |
height & width | Sets the width and height of the video in CSS pixels |
loop* | Tells the browser to automatically loop the video |
muted* | Mutes the audio from the video |
Subscribe to:
Posts (Atom)