Friday, December 6, 2013

Calling javascript function from php

Calling javascript function from php

I am trying to call a javascript function from php. According to all of the examples I have been looking at the following should work but it doesn't. Why not?

  
<?php
    echo "function test";
    echo '<script type="text/javascript">    run();      </script>';
?> 
 
 <html>
    <script type="text/javascript">
        function run(){
            alert("hello world");
        }
    </script>
</html>

 

Monday, December 2, 2013

Time Difference in Php

How to calculate minute difference between two date-times in PHP?


<?

$to_time = strtotime("2008-12-13 10:42:00");
$from_time = strtotime("2008-12-13 10:21:00");
echo round(abs($to_time - $from_time) / 60,2). " minute";
 
 ?>
 
 
 
<?php
$date1 = time();
sleep(2000);
$date2 = time();
$mins = ($date2 - $date1) / 60;
echo $mins;
?>
 

php function to remove special characters

 php function to remove special characters
 
 
function clean($string) {
   $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
   return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
}
 
 
 
 
how can I prevent multiple hyphens from being next to each other? 
and have them replaced with just 1? Thanks in advance! 

function clean($string) {
   $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
   $string = preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.

   return preg_replace('/-+/', '-', $string); // Replaces multiple hyphens with single one.
}

Wednesday, October 9, 2013

Mysql Time Convert to GMT Format Example

 
It looks like the TZ of your database is set to GMT (UTC), which is as it should be. You need to convert your local date into GMT when adding to the database.
from the MySQL 5.1

CONVERT_TZ(dt,from_tz,to_tz)
 
CONVERT_TZ() converts a datetime value dt from time zone given by from_tz to the time zone given by to_tz and returns the resulting value.
 Time zones may be specified as described in Section 5.10.8, “MySQL Server Time Zone Support”. This function returns NULL if the arguments are invalid.

If the value falls out of the supported range of the TIMESTAMP type when converted fom from_tz to UTC, no conversion occurs. The TIMESTAMP range is described in Section 11.1.2, “Overview of Date and Time Types”.

mysql> SELECT CONVERT_TZ('2004-01-01 12:00:00','GMT','MET');
        -> '2004-01-01 13:00:00'

mysql> SELECT CONVERT_TZ('2004-01-01 12:00:00','+00:00','+10:00');
        -> '2004-01-01 22:00:00'

Wednesday, September 18, 2013

Loops in PHP

Introduction to Loops in PHP

Using Loops in PHP

Loops are an vital part of a programming language. Typically the need to use loops in a program occur when you want to execute a piece of code repeatedly until a desired condition is achieved. There are many control structures that can be use to implement looping in a program.

The While loop

The while loop is one is the simplest looping control structure in PHP. When you specify a condition and a code block along with the while loop, the code block is repeatedly execute until the condition becomes false.

<?php
$a = 1;
while ($a <= 10){
  echo $a ."<br/>";
}
?>

The for loop

The for loop is similar to the while loop and can achieve the same result. It is use to evaluate a set of conditional expression at the beginning of each loop. The for loop is one of the most complex loop in PHP.

<?php
for ($a=1; $a <=10 ; $a++){
  echo $a ."<br/>";
}
?>


Breaking a loop

Sometimes, you might want to break a loop before it complete execution .The break statement can do this.

<?php
for ($a=1; $a <=10 ; $a++){
echo $a ."<br/>";
  if($a==5){
    break;
  }
}
?>

The continue statement

The continue statement is the reverse  the break statement, doesn't breaks a loop. When you break a loop the loop with the break statement, you can't check for the same condition again. The continue statement just skip the specific iteration and doesn't cause the loop to end.

<?php
for ($i = 0; $i < 5; ++$i) {
if ($i == 2)
continue
  echo $i;
}
?>

The output will be: 2.

Nesting loops

Nesting loops are a way to use a loop statement within another statement. for example, if you have a loop x, and have another loop y in the loop y in the loop x, the loop y will be executed x*y.

<?php
for ($a =10; $a <= 15; $a++){
    echo "First Loop <br/>";
       for ($b = 5; $b < 10; $b++){
           echo "Second Loop <br/>";
       }
}
?>

Tuesday, September 17, 2013

PHP function to get age for date of birth

 PHP function to get age for date of birth  
 
function age_from_dob($dob){
 
$dob = strtotime($dob);
$y = date('Y', $dob);
 if (($m = (date('m') - date('m', $dob))) < 0) {
  $y++;
 } elseif ($m == 0 && date('d') - date('d', $dob) < 0) {
  $y++;
 }
return date('Y') - $y;
 } 
 
 
 
 echo age_from_dob('2005/04/19'); date in yyyy/mm/dd format.

Tuesday, August 20, 2013

substring_index string function

substring_index string function

Substring_index function returns us a substring from the main string from a landmark which is given by delimiter. From the delimiter to left or right ( towards staring or ending ) of the string can be given by count. If count is positive then everything towards left from final delimiter ( from left ) is returned. If the count is negative then everything towards right from final delimiter is returned.

Here is the syntax

substring_index(string,"delimiter”,count )

We will apply this to our newsletter subscriber table where we stored email address of our subscribers. Here we will apply substring_index to separate domain part and userid part and maintain a list. Here is a sample of email address.

userid@domainname.com

Here is the query to get the

select email,substring_index(email,"@",-1) from newsletter_table

This will give output as

userid@aol.com aol.com
onemore@gmail.com gmail.com

Like this the full list can be displayed. We can use count and group by command to generate a query to which can tell us number of subscribers in each domain. Say how many have yahoo account, how many have gmail account etc….

SELECT count(substring_index(email,"@",-1) ) as no, email FROM `newsletter_table` group by substring_index(email,"@",-1) order by no desc

Labels

AJAX (1) Answers (1) Apache (1) Array (16) bug (1) C (1) C++ (1) Calendar (1) Class (1) Commands (1) Cookies (2) Database (2) Date (7) Days (1) explode (1) File Upload (1) FILES (1) firewall (1) fix (1) Functions (26) GET (1) GMT (1) JavaScript (2) localhost (1) Mail (1) Menu (1) MYSQL (13) PERL (1) PHP (36) php.ini (1) POST (1) preg_match (1) Questions (1) Script (1) SMS (2) Solution (1) String (1) Time (5) Time Zone (1) Vista (1) Wamp Server (1) windows 7 (2) XML (1)

Popular Posts

Popular Posts