/ Gists / PHP

Gists - PHP

On gists

Kontrola BOOMu na začátku souboru

PHP

UTF-8 BOOM check #

echo "\xEF\xBB\xBF"; // UTF-8 BOM

On gists

Validation date & time

PHP

PHP: Date validation (datetime) #

function validateDate($date, $format = 'Y-m-d H:i:s')
{
    $d = DateTime::createFromFormat($format, $date);
    return $d && $d->format($format) == $date;
}

var_dump(validateDate('2012-02-28 12:12:12')); # true
var_dump(validateDate('2012-02-30 12:12:12')); # false
var_dump(validateDate('2012-02-28', 'Y-m-d')); # true
var_dump(validateDate('28/02/2012', 'd/m/Y')); # true
var_dump(validateDate('30/02/2012', 'd/m/Y')); # false
var_dump(validateDate('14:50', 'H:i')); # true
var_dump(validateDate('14:77', 'H:i')); # false
var_dump(validateDate(14, 'H')); # true
var_dump(validateDate('14', 'H')); # true

var_dump(validateDate('2012-02-28T12:12:12+02:00', 'Y-m-d\TH:i:sP')); # true
# or
var_dump(validateDate('2012-02-28T12:12:12+02:00', DateTime::ATOM)); # true

var_dump(validateDate('Tue, 28 Feb 2012 12:12:12 +0200', 'D, d M Y H:i:s O')); # true
# or
var_dump(validateDate('Tue, 28 Feb 2012 12:12:12 +0200', DateTime::RSS)); # true
var_dump(validateDate('Tue, 27 Feb 2012 12:12:12 +0200', DateTime::RSS)); # false
# ...

On gists

Date range - vygenerování datum. rozmezí

PHP

array of date range.php #

<?php 

function dateRange( $first, $last, $step = '+1 day', $format = 'Y/m/d' ) {

	$dates = array();
	$current = strtotime( $first );
	$last = strtotime( $last );

	while( $current <= $last ) {

		$dates[] = date( $format, $current );
		$current = strtotime( $step, $current );
	}

	return $dates;
}

?>

On gists

PHP: CSV export from MySql to output

PHP

csv-export.php #

<?
    $out = '';
    $fields = dibi::fetchAll("SHOW COLUMNS FROM web_application");
    foreach ($fields as $field)
    {
    $out .= '"'.$field->Field.'";';
    }
    $out = rtrim($out, ';');
    $out .= PHP_EOL;
    foreach ($this->model->csvExport() as $index => $row)
    {
    foreach ($row as $r)
    {
    $out .='"'.$r.'";';
    }
    $out = rtrim($out, ';');
    $out .= PHP_EOL;
    }
    $out = iconv('utf-8', 'windows-1250', $out);
    $filename = 'application-export___' . date('YmdHis') . '.csv';
    header('Content-Encoding: windows-1250');
    header('Content-type: text/csv; charset=windows-1250');
    header('Content-Disposition: attachment; filename=' . $filename);
    //echo "\xEF\xBB\xBF"; // UTF-8 BOM
    echo $out;
    exit(0);
    $this->view->setTemplateFile('modules/application/csv.php');

On gists

PHP: Readable filesize

PHP Helpers-Filters-Plugins

readable-filesize.php #

<? 

function HumanReadableFilesize($size) {
// Adapted from: http://www.php.net/manual/en/function.filesize.php
$mod = 1024;
$units = explode(' ','B KB MB GB TB PB');
for ($i = 0; $size > $mod; $i++) {
$size /= $mod;
}
return round($size, 2) . ' ' . $units[$i];
}

function fsize($file) {
$a = array("B", "KB", "MB", "GB", "TB", "PB");
$pos = 0;
$size = filesize($file);
while ($size >= 1024) {$size /= 1024;$pos++;}
return round($size,2)." ".$a[$pos];
}

?>

On gists

PHP: Unzip folder

PHP

unzip-folder.php #

<?
    $extract_dir = "./sprites/";
    $extract_file = "sprites.zip";
    $zip = new ZipArchive;
    $res = $zip->open($extract_file);
    if ($res === TRUE) {
    $zip->extractTo($extract_dir);
    $zip->close();
    echo "OK";
    } else {
    echo "NOK";
    }

?>

On gists

PHP: Zip folder

PHP

zip-folder.php #

<?    
class zipuj_helper
    {
    protected $jmeno_zipu;
    protected $root;
    protected $zip;
    public function __construct($root = ".", $jmeno_zipu = "zip.zip")
    {
    $this->root = $root;
    $this->jmeno_zipu = $jmeno_zipu;
    $this->zip = new ZipArchive();
    $this->zip->open($this->jmeno_zipu, ZIPARCHIVE::CREATE);
    $this->nactiAdr();
    $this->uloz();
    }
    public function nactiAdr($cesta = "")
    {
    $hn = scandir($this->root.$cesta);
    foreach ($hn as $file)
    {
    if ($file == "." || $file == "..")
    {
    continue;
    }
    if (is_dir($this->root.$cesta."/".$file))
    {
    $this->zip->addEmptyDir($cesta."/".$file);
    $this->nactiAdr($cesta."/".$file);
    }
    else
    {
    $this->zip->addFile($this->root.$cesta."/".$file, $cesta."/".$file);
    }
    }
    }
    public function uloz()
    {
    $this->zip->close();
    }
    }

    // use
 $filename = 'prilohy__' . date('YmdHis') . '.zip';
 $zalohuj = new zipuj_helper('./storage/application/', './storage/application-zip/' . $filename);