Wednesday, October 13, 2010

How to synchronize MySQL time with server time?

This command

SET GLOBAL time_zone = 'SYSTEM';

If you can edit my.cnf file, set MySQL default timezone with this line

[mysqld]
default-time-zone='SYSTEM'

How to save progressive JPEG with GD Library?



PHP imageinterlace() funtion turns the interlace bit on or off.
If the interlace bit is set and the image is used as a JPEG image, the image is created as a progressive JPEG.

<?php
// Create an image instance
$im = imagecreatefromjpeg('test.jpg');

// Enable interlancing
imageinterlace($im, true);

// Save the interlaced image
imagegif($im, './progressive.jpg');
imagedestroy($im);
?>

How to [recursively] Zip a directory in PHP?

You can Zip a directory with ZipArchive Class in PHP easily.


<?php
function archive($path, $archive, $pathcleaning = false){
$zip = new ZipArchive();
if ($zip->open($archive, ZIPARCHIVE::CREATE) !== TRUE) {  die ("Could not open archive");  }
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
foreach ($iterator as $key=>$value) 
if(basename($key) != '.' && basename($key) != '..'){
$zip->addFile(realpath($key), $pathcleaning ? str_replace($path, '', $key) : $key) or die ("ERROR: Could not add file: $key");
}
$zip->close();
}


// zip a directory
archive('data/', 'data.zip', true);


// get the zip file
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="data.zip"');
header('Content-Transfer-Encoding: binary');
readfile('data.zip');
?>

How to create subdomains using htaccess?

First, you should set all subdomains to one directory in your web server config.

like this one below: 
ServerName  mydomain.com
ServerAlias   *.mydomain.com
Second, add this code to your .htacess file

RewriteEngine on
# SUB DOMAINS
RewriteCond %{http_host} !www\.([a-z0-9-]+)\.([a-z0-9-]{2,5})$ [NC]
RewriteCond %{http_host} ^(www.)?([a-z0-9-]+)\.([a-z0-9-]+)\.([a-z0-9-]{2,5})$ [NC]
RewriteRule (.*) /clients/%2/$1 [L]
# /SUB DOMAINS 


Finally, you should create a /clients/ folder in the main directory. Now every folder under /clients/ folder is a subdomain
For ex. If you have a test/ folder under the /clients/ folder, you can call the folder test.mydomain.com


How to change URL without refreshing?

You can change the URL without refreshing  with  location.hash  .

For ex: facebook and twitter use this method.

http://twitter.com/#!/username

Browsers use the # symbol as page anchors and you create your own prefix to catch the URL like #!
You can  listen to URL with  window.onhaschange  to send a XHTTP request when it changes.

Example:
<script type="text/javascript"> 
window.onhashchange = function() {
    var link = location.hash.replace('#!','');
// add  your XHTTP request code here
    alert('URL changed. Your link is "'+ link +'"');
}
</script>
<a href="#!/en/home/">change url</a>

If you wish to check browsers' onhaschange capability, you can use the code below:


<script type="text/javascript">
if ("onhashchange" in window) { alert('location hash is enable!'); } 
</script> 

How to detect screen size in CSS?

CSS3 Media Queries

Max Width
The following CSS will apply if the viewing area is smaller than 500px.
<style type="text/css">
@media screen and (max-width: 500px) {
.class { background: #ccc; }
}
</style>

If you want to link to a separate stylesheet, put the following line of code in between the <head> tag.
<link rel="stylesheet" media="screen and (max-width: 500px)" href="small.css" />

Min Width
The following CSS will apply if the viewing area is greater than 800px.
<style type="text/css">
@media screen and (min-width: 800px) {
.class { background: #666; }
}
</style>

Multiple Media Queries
You can combine multiple media queries. The following code will apply if the viewing area is between 500px and 800px.
<style type="text/css">
@media screen and (min-width: 500px) and (max-width: 800px) {
.class { background: #333; }
}
</style>

* Media query is not supported in Internet Explorer 8 or older. You can use Javascript to hack around.

Example:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Media Query Demos</title>
<style type="text/css">
.box { margin:10px; padding:10px; border:1px solid #666;}
/* max-width */
@media screen and (max-width: 500px) {
.max { background: #18d; }
}
/* min-width */
@media screen and (min-width: 800px) {
.min { background: #18d; }
}
/* min-width & max-width */
@media screen and (min-width: 500px) and (max-width: 800px) {
.between { background: #18d; }
}
</style>
</head>
<body>
<div class="box max">This box will turn to blue if the viewing area is less than 500px</div>
<div class="box min">This box will turn to blue if the viewing area is greater than 800px</div>
<div class="box between">This box will turn to blue if the viewing area is between 500px and 800px</div>
</body>
</html>

How to round numbers in JavaScript?

You can use Math Object or Bitwise Operator for round a number:

round(a) - Returns the value of a to the nearest integer. If the values decimal value is .5 or greater the next highest integer value is returned otherwise the next lowest integer is returned.


floor(a) - Rounds the passed value down to the previous integer. If the passed value is already an integer the returned value is the same as the passed value.


ceil(x) - Rounds up the value of "a" to the next integer. If the value is a already whole number, the return value will be the same as the passed value.

<script type="text/javascript">
var original=28.453;
//round "original" to the nearest integer
var result = Math.round(original);  //returns 28
//other way
var result = original >> 0;  //returns 28

//round "original" to  down to the previous integer
var result = Math.floor(original);  //returns 28
//round "original" to  up to the next integer
var result = Math.ceil(original);  //returns 29

//round "original" to two decimals
var result = Math.round(original*100)/100;  //returns 28.45
// other way
var result = (original *100 >> 0) *0.01;  //returns 28.45
</script>

How to remove focus border of browsers?

Some browsers add an outline on the element when you focus it. ex: Firefox
If you don't want it, you can remove it via a CSS trick:

<style type="text/css">
:focus { outline: none; }
</style>

How to HTML5 tags working in IE8?

You can get HTML5 tags working in IE8 by including this JavaScript in the head.

<script type="text/javascript">
 document.createElement('header');
 document.createElement('hgroup');
 document.createElement('nav');
 document.createElement('menu');
 document.createElement('section');
 document.createElement('article');
 document.createElement('aside');
 document.createElement('footer');
</script>