• Home
  • Biography
  • chop.com.au

Archive for the ‘Code Snippet’ Category.

SQL Snippet: Update table field using a join using SQL

September 8, 2010, 10:52 am  |  Posted in: Code Snippet, Dev, Uncategorized

This is a handy little snippet if you need to update one field (source) in one table (table 1) from the results of a field in a related table (table 2)


UPDATE table1 JOIN table1 ON table1.id = table2.id SET table2.source = table1.source
Comment

PHP – having problems with getimagesize() when space in filename?

April 29, 2010, 7:09 pm  |  Posted in: Code Snippet, Dev, PHP

Some images containing spces in the file name led me to some errors when trying to determine their image size for manipulation using PHP.

Simply url encoding the string fixed this problem.
 

 <?php
  $imagePath = "some funky filename.jpg";
  $imagePath = str_replace(" ","%20",$imagePath);
  list($imgwidth,$imgheight) = getimagesize($imagePath);
?>
Comment

PHP Snippet – GetFileContents and save PDF

April 3, 2010, 2:51 pm  |  Posted in: Code Snippet, Dev, PHP

I needed to be able to access a pdf outside my document root… so I created the script below. Using just file_get_contents with an echo would just save it as the name of the script, using an $output_file variable and in the header you are able to specify what you want to save it as.


<?php
$output_file = 'outfilename.pdf';
$file = "target.pdf";
$fileDir = '../target/directory/';
$contents = file_get_contents($fileDir.$file);
header("Content-Disposition: attachment; filename=" . $output_file);
header('Content-type: application/pdf');
echo $contents;
exit();
?>
Comment

PHP Snippet – Get Yesterdays date

February 27, 2010, 2:43 pm  |  Posted in: Code Snippet, Dev, PHP

This is a simple function that returns yesterdays date. Simple but handy.

function yesterday()
{
 $yesterday = date("Y:m:d",mktime(0,0,0,date("m") ,date("d")-1,date("Y")));
 return $yesterday;
}
Comment

PHP Snippet – Time difference between 2 date/time stamps.

February 27, 2010, 2:03 pm  |  Posted in: Code Snippet, Dev, PHP

In a recent project I needed to capture how long the user spent on the application, it was not expected they would spend days on it so it return hours minutes seconds which can be saved to a database.
So basically here is a function to do that…

//
echo getTimeDifference("2010-02-26 18:35:36");

function getTimeDifference($start_time){

 //convert $start_time into a usable string
 $tempStart = preg_replace('/(\d{2})-(\d{2})-(\d{2})(.*)/', '$1:$2:$3:$4',$start_time);
 $tempStart=explode(":",$tempStart);

 $year = $tempStart[0];
 $month= $tempStart[1];
 $day = $tempStart[2];
 $hour = $tempStart[3];
 $minute = $tempStart[4];
 $second = $tempStart[5];

 //set the end time as current time
 $timesEnd = date("Y:m:d:H:i:s");
 $tempEnd=explode(":",$timesEnd);

 $year1 = $tempEnd[0];
 $month1= $tempEnd[1];
 $day1 = $tempEnd[2];
 $hour1 = $tempEnd[3];
 $minute1 = $tempEnd[4];
 $second1 = $tempEnd[5];

 $countdown_date = mktime($hour, $minute, $second, $month, $day, $year);
 $countdown_date1 = mktime($hour1, $minute1, $second1, $month1, $day1, $year1);

 $diff = $countdown_date1 -$countdown_date ;
 if ($diff < 0)
 $diff = 0;
 $dl = floor($diff/60/60/24);
 $hl = floor(($diff - $dl*60*60*24)/60/60);
 $ml = floor(($diff - $dl*60*60*24 - $hl*60*60)/60);
 $sl = floor(($diff - $dl*60*60*24 - $hl*60*60 - $ml*60)
 );

 $timeDifference = "$hl:$ml:$sl";

 return $timeDifference;
 }
Comment
  • Author

    Leon Wilson spends most of his time designing and developing flash sites, and wishes he was better at high end 3D.

  • Download V Card

  • Categories

    • 3D
    • Dev
      • Code Snippet
      • PHP
    • Flash
    • Freelance
    • General Banta
    • Graphic Design
    • Identity
    • Rumbles & Mumbles
    • Uncategorized
    • Website
  • Archives

    • September 2010
    • August 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • November 2009
    • September 2009
    • August 2009
    • June 2009
    • May 2009
    • April 2009
    • February 2009
Copyright © 2010 Leon Wilson. All rights reserved.
Powered by WordPress.