Tag Archives: php

date difference or count day or month or year between two dates in php

If you have a get a date difference between two dates like 2 days left or 1 month left count from current date to start date than it code is useful for you..


$date2 = date(“d-m-Y H:i:s”,strtotime($row[‘end_date_time’]));
// where $row[‘end_date_time’] is your start data
$date1 = date(“d-m-Y H:i:s”,strtotime(date(‘Y-m-d H:i:s’)));

$diff = abs(strtotime($date2) – strtotime($date1));

$years   = floor($diff / (365*60*60*24));
$months  = floor(($diff –
$years * 365*60*60*24) / (30*60*60*24));
$days    = floor(($diff –
$years * 365*60*60*24 – $months*30*60*60*24)/ (60*60*24));
$hours   =
floor(($diff – $years * 365*60*60*24 – $months*30*60*60*24 –
$days*60*60*24)/ (60*60));
$minuts  = floor(($diff – $years *
365*60*60*24 – $months*30*60*60*24 – $days*60*60*24 – $hours*60*60)/
60);  $seconds = floor(($diff – $years * 365*60*60*24 –
$months*30*60*60*24 – $days*60*60*24 – $hours*60*60 – $minuts*60));

printf(“%d years, %d months, %d days, %d hours, %d minuts\n, %d
seconds\n”, $years, $months, $days, $hours, $minuts, $seconds);


google map with calculate between more than two places in php

$travel_location = ‘1st main Road&Peenya Industrial Area’;

$bootloca = explode(‘&’,$travel_location);
$origins = str_replace(‘ ‘, ‘+’, $_REQUEST[‘city’]);
$destinations = ”;
foreach($bootloca as $book):
$destinations = $destinations.str_replace(‘ ‘, ‘+’, $book).’|’;



//print ‘<pre>’;
//print_r($json);   // for check array value
//print ‘</pre>’;
$total_distance = 0;
foreach($json[“rows”][0][“elements”] as $dist):

$total_distance += ceil($dist[“distance”][“text”]);


print $total_distance;

how to access Joomla 2.5!: Classes for Accessing Database for mysql

For select Query:

$db = JFactory::getDBO();
$query = $db->getQuery(true);
$results = $db->loadObjectList();
if ($results){
    foreach($results as $result) 
For insert Query:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->set("field1='value1', fieldn='valuen'");
To get the last insert id, use:

For udate Query:
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->set("field1='value1', fieldn='valuen'");
For simple query: 
$db = JFactory::getDbo();
$query = "<YOUR QUERY>";

For fetch multiple row:

Your Query goes here
$items = $db->loadObjectList();

For fetch Single row:

Your Query goes here
$book_item = $db->loadObject();

array value insert table in php with mysql

<form name=’frm’ method=”post” action=’#’>

<input type=”text” name=”id[]” value=””  />

<input type=”text” name=”name[]” value=””  />

<input type=”submit” id=”submit” value=”submit” name=”submit” />


in php code

foreach ($_POST[‘id’] as $row=>$id)

$id = $id;

$name = $_POST[‘name’][$row];

print ‘insert into table name value(‘.$id.’,”‘.$name.'”)’;


Get last record using php or php with mysql

For example: In this example explode commas and make a linkable every word.

$ash = ‘1,2,3,4’;
$ashval = explode(‘,’,$ash);

foreach($ashval as $val)
print “<a href=”>”.$val.”</a>”;
print ‘,’;
print ”;


OR you can use another method:

$ash = ‘1,2,3,4’;
$ashval = explode(‘,’,$ash);

foreach($ashval as $val)

print “<a href=”>”. $val.”</a>”.$te;


ATM Machine logic in PHP.

Q. Consider a hypothetical ATM machine which gives you notes of 10, 20, 50, 100, 500 & 1000 in denomination. If I enter 5,550 Rs. It should give me 5 notes of 1000, 1 note of 500 and 1 note of 50. ATM machines cannot store coins. So, if input amount is something like 1978 then machine should print “Invalid Amount”.

A .


$rupees = array(1000,500,100,50,20,10);
$count = array(0,0,0,0,0);

if($rupees[$i]<$amount || $rupees[$i]==$amount):
print ‘<b>Input=</b>’.$_REQUEST[‘amount’].'<br>’;



echo ($rupees[$i].’ * ‘.+$count[$i] .’=’. ($rupees[$i]*$count[$i])).'<br>’;
$tot += $rupees[$i]*$count[$i];
print ‘<b>Total=</b>’.$tot;
print ‘<b>Invalid Amount ‘.$_REQUEST[‘amount’].'</b>’;

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>AIM MACHINE</title>

<form name=”frm” method=”post” action=”#”>
Enter value:<input type=”text” name=”amount” value=”” />
<input type=”submit” name=”submit” value=”submit” />

Display Indian current time in PHP

This small snippet explains how to get Indian Time in PHP without changing the server times and timezones. So irrespective of whether your server is in PST or GMT, you can still display IST when displaying this with PHP.

$timezone = new DateTimeZone("Asia/Kolkata" );
$date = new DateTime();
$date->setTimezone($timezone );
echo  $date->format( 'H:i:s A  /  D, M jS, Y' );

Here is a list of supported timezones in PHP for your reference.

for loops to create a multiplication table for php

$start = 1;
$end = 10;

print ‘<table border=”1″ cellspacing=”0″ cellpadding=”5″>’;
for ($y = $start – 1; $y <= $end; ++$y) {
print “<tr>”;
for ($x = $start – 1; $x <= $end; ++$x) {
if ($y == $start – 1) {
print “<td><b>$x</b></td>”;

} else if ($x == $start – 1) {
print “<td><b>$y</b></td>”;
} else {
print “<td>”;
print $x*$y;
print “</td>”;
print “</tr>”;
print “</table>”;?>

joomla store value Using the JTable Class

The first step is to extend the JTable class with your new table class in table folder:

class TableRestaurant extends JTable 
   var $id = null; 
   var $name = null; 
   var $address = null; 
   var $comments = null; 
   var $comment_date = null;
   var $published = null; 
   function __construct(&$db) 
      parent::__construct( '#__restaurant', 'id', $db );

To include the file, place this line in your extension's source code (use com_nameofyourcomponent in place of com_restaurant):
To get an instance of the object, use this code:
$row =& JTable::getInstance('restaurant', 'Table');

In a model class (extends JModel) you can also use:

$row =& $this->getTable('restaurant');

Updating is as simple as setting the row values in this manner.

$row->name = 'ashwin';
$row->address = 'test address';
$row->comments = 'Open Source Developer';
// Save results

Notice that the $row object of type TableRestaurant has all the columns we defined above.  When we call the store() method these rows are automatically stored in the database.  Our code above assumes that the id field will be auto-incremented on store(), so we are not providing a value for this field (as well as comment_date and published).  So this code will store a new value for this restaurant (essentially running and 'INSERT' query for us).  If we had provided value for the id field, the store() method would have instead 'UPDATE' query.

random coupon generator in php

	$passes = array();
 	$chars = "abcdefghkmnopqrstuvwxyz023456789";
	$amount = 100;
	$lenght = 6;   

	for($j=0; $j<$amount; $j++)

		 $i = 0; $pass = '' ;
		 while ($i <= $length)
		 { $num = rand() % 33; $tmp = substr($chars, $num, 1);
		 $pass = $pass . $tmp; $i++;
		 $passes[] = $pass;

	foreach($passes as $password)
		echo strtoupper(substr(md5($password),0,15))."<br />";