PHP have two families of date functions.
date(), time(), mktime() functions calculate time in current server time zone. These functions complicated, when need compare time in different time zones. For this kind task php have gmtime() gmdate() gmmktime() functions, these functions return Greenwich Mean Time, which are same in all world and ignore current server time zone.
You can change time zone with date_default_timezone_set function. Supported time zones list you can find here
Wednesday, December 30, 2009
PHP: simple but powerful file upload class
Today I share with you idea how handle uploading files. First you need html form, with proper enctype.
Then download FileUpload class from my github repository.
Create upload.php file.
File will be uploaded to uploads directory (must be writeable).
Then download FileUpload class from my github repository.
Create upload.php file.
File will be uploaded to uploads directory (must be writeable).
PHP: how validate email
Regexp to validate email
First example validate only email syntax, second check if DNS MX record exists.
First example validate only email syntax, second check if DNS MX record exists.
Tuesday, December 29, 2009
PHP: watermarks
Maybe you have trouble to put png watermark on jpeg image? I share with you simple solution. You can found it on my github repository . Git is very good solution for code managment. But about it I tell you later.
How to check whick php version you are running
phpinfo() function will print nice table, where you can see all php settings. Also this function usefull when need check installed extensions.
PHP: how delete folder with subfolders
Sometimes need delete whole directory with subfolders and files (Example cache). Standart function rmdir can delete only empty folder. I provide for you simple but powerfull function, which make your life much easier.
function _rmdir($dir) {
if (file_exists($dir)){
$files = glob( $dir . '*', GLOB_MARK );
foreach( $files as $file ){
if( substr( $file, -1 ) == '/' )
_rmdir( $file );
else
unlink( $file );
}
if (is_dir($dir)) rmdir( $dir );
}
}
code at github.com
function _rmdir($dir) {
if (file_exists($dir)){
$files = glob( $dir . '*', GLOB_MARK );
foreach( $files as $file ){
if( substr( $file, -1 ) == '/' )
_rmdir( $file );
else
unlink( $file );
}
if (is_dir($dir)) rmdir( $dir );
}
}
code at github.com
PHP: How get the number of days in month
Easiest way to get number of days in month are to use date function.
echo date("t",mktime(0,0,0,$month,1,$year);
Also this will work with leap year.
echo date("t",mktime(0,0,0,$month,1,$year);
Also this will work with leap year.
PHP: sort an array with a user-defined comparison function
This is the way to sort an array with the user-defined function
$a = array(
'Alex'=>array('age'=>22,'hoby'=>'basketball'),
'John'=>array('age'=>21,'hoby'=>'soccer'),
'Ivan'=>array('age'=>30,'hoby'=>'vodka'),
);
// creating user-defined function
$newF = create_function('$item1,$item2',
'return ($item1["age"]<$item2["age"])?true:false;');
usort($a,$newF);
var_dump($a);
$a = array(
'Alex'=>array('age'=>22,'hoby'=>'basketball'),
'John'=>array('age'=>21,'hoby'=>'soccer'),
'Ivan'=>array('age'=>30,'hoby'=>'vodka'),
);
// creating user-defined function
$newF = create_function('$item1,$item2',
'return ($item1["age"]<$item2["age"])?true:false;');
usort($a,$newF);
var_dump($a);
Subscribe to:
Comments (Atom)
+-+Mozilla+Firefox.png)

