Skip to main content

Posts

Days difference

Ever wanted to know the number of days that have been passed since the beginning of this 2010 and the number of days passed since your birthday ? Then try this code: <?php function noofdays($day_1,$day_2) { $diff = strtotime($day_2) - strtotime($day_1) + 1; //Find the number of seconds $day_difference = ceil($diff / (60*60*24)) ; //Find how many days that is return $day_difference; } $bday='1985-06-18'; // My birthday, replace it with yours' $day1=date("Y-01-01"); // Start date of current year $day2=date("Y-m-d"); // Today's date //$diff = strtotime($day2) - strtotime($day1) + 1; //Find the number of seconds //echo ceil($diff / (60*60*24)) ; //Find how many days that is $x= noofdays($day1,$day2); $y= noofdays($bday,$day2); echo 'No of days passed this year = '.$x; echo '<br>'; echo 'No of days passed from my birthday till date = '.$y; ?>

File and Dir names

If you ever wanted to know the file and directory names that you are working upon, this piece of code might help you. <?php // __FILE__ represents C:\xampp\htdocs\dirname.php echo 'The current file: '.'<b>'.__FILE__.'</b><br><br>'; echo 'DIR name using dirname(__FILE__): '.'<b>'.dirname(__FILE__).'</b><br><br>'; // returns C:\xampp\htdocs echo 'Current working directory using getcwd(): '.'<b>'.getcwd().'</b><br><br>'; // returns C:\xampp\htdocs echo 'Basename of current file: '.'<b>'.basename(__FILE__).'</b><br><br>'; //returns dirname.php echo 'Basename of current directory: '.'<b>'.basename(dirname(__FILE__)).'</b><br><br>'; //returns htdocs $arrStr = explode("/", $_SERVER['SCRIPT_NAME'] ); $arrStr = array_reverse($arrStr ); ec...

Convert & Calculate the length of an flv video

You can convert an flv video into a wmv video using the command line: ffmpeg -i test.flv test.wmv Through the above command, the "test.flv" video file is converted to "test.wmv" video file. Note that you need the file "ffmpeg.exe" in the folder from where you invoke the above command. And you can dynamically calculate the length of an flv video, using the function below: <?php function GetFLVDuration($file){ // get contents of a file into a string if (file_exists($file)){ $handle = fopen($file, "r"); $contents = fread($handle, filesize($file)); fclose($handle); // if (strlen($contents) > 3){ if (substr($contents,0,3) == "FLV"){ $taglen = hexdec(bin2hex(substr($contents,strlen($contents)-3))); if (strlen($contents) > $taglen){ $duration = hexdec(bin2hex(substr($contents,strlen($contents)-$taglen,3))) ; return secondsToWords($duration/1000); } } ...

OOPS way of querying database !!!

Tired of writing those connect and execute query statements in php all the time ? Now you have got an OOPS way of executing your queries... Using this, there are many advantages like avoiding the reuse of code and improvement in performance. Supose we have a user table with the fields id, name, branch and city: CREATE TABLE IF NOT EXISTS `user` ( `id` int(2) NOT NULL auto_increment, `name` varchar(25) NOT NULL, `branch` varchar(25) NOT NULL, `city` varchar(25) NOT NULL, PRIMARY KEY (`id`) ) Dump few values into it: INSERT INTO `user` (`id`, `name`, `branch`, `city`) VALUES (1, 'ravi', 'CSE', 'Hyderabad'), (2, 'kiran', 'Mech', 'Vijayawada'), (3, 'prasad', 'CSIT', 'Vizag'), (4, 'sashi', 'ECE', 'Tirupati'); Now to perform the task, the following are the steps: 1) First create a config file with the details of the constants like: host, username, password, database name and all the tab...

Form validations before submitting it

Try to use 'OnSubmit' attribute of the form tag in your page, for submitting the page. We use the action attribute of the form to specify the destination page for the current page. But before submitting, we generally want to validate few fields in the form. And if we use something like: onSubmit="return valid();" , the form submission happens only after the function valid() returns true, and we can perform all validations inside the function. Ex: function valid() { var d = document.form1; if(d.email.value ==""){ alert("Please enter the email"); d.email.focus(); return false; } return true; }

Oscommerce - Dont show latest products at the bottom

In Oscommerce, generally the latest products (which may be added/edited) are shown at the bottom of the categories page, below the sub categories. To avoid this, simply comment the code in the while loop of the file: includes/modules/new_products.php The code will be something like: while ($new_products = tep_db_fetch_array($new_products_query)) { ..... Now you dont find any single products when you open a category.