Skip to content

Instantly share code, notes, and snippets.

View cp6's full-sized avatar
🏠
Working from home

corbpie cp6

🏠
Working from home
View GitHub Profile
@cp6
cp6 / table_func.php
Created September 24, 2020 03:41
PHP function for making HTML tables
<?php
function outputString(string $string)
{
echo $string;
}
function tableBuilder(array $theads, array $data, string $table_class = 'table', string $thead_class = '')
{
(empty($table_class)) ? $tbl = "" : $tbl = " class='$table_class'";
(empty($thead_class)) ? $th = "" : $th = " class='$thead_class'";
@cp6
cp6 / page_system.php
Created September 1, 2020 06:11
PHP MySQL pagination system function
<?php
function MysqlPageSystem(int $page_number, int $items_per_page)
{
if ($page_number == 0) {
$start_at = 0;//Items 0 to $items_per_page
} else {
$start_at = ($page_number * $items_per_page);//Assume itemsPP = 4. For Page 2 (1*4) so it shows items 4 to 8
//For Page 3 (?page=2) equation is (2*4) so page=2 will show items 8 to 12
}
@cp6
cp6 / ftp_class.php
Created August 23, 2020 01:51
Simple PHP FTP class
<?php
class doFTP
{
public string $hostname = '111.222.33.4';
public string $username = 'ftp_user';
public string $password = '9dgVGOI6dG';
public bool $passive_mode = true;
public function setDetails(string $hostname, string $username, string $password, bool $passive_mode)
@cp6
cp6 / ffmpeg_duration.php
Created August 21, 2020 14:26
ffmpeg time cut duration calculation function
<?php
function timeToSeconds(string $time): int
{
$arr = explode(':', $time);
return $arr[0] * 3600 + $arr[1] * 60 + $arr[2];
}
function duration(string $start, string $end): string
{
$length = (timeToSeconds($start) - timeToSeconds($end));
@cp6
cp6 / convert_to_seconds.php
Created August 21, 2020 14:04
PHP converting hours, minutes & seconds string to seconds
<?php
function timeToSeconds(string $time): int
{
$arr = explode(':', $time);
if (count($arr) == 3) {
return $arr[0] * 3600 + $arr[1] * 60 + $arr[2];
} else {
return $arr[1] * 60 + $arr[3];
}
}
@cp6
cp6 / array_return.php
Last active August 10, 2020 03:21
Vultr instance id for instance label API v2
<?php
function idForLabel(string $label, array $instance_data)
{
foreach ($instance_data['instances'] as $instance) {
if ($instance['label'] == $label) {
return array('found' => true, 'id' => $instance['id'], 'searchLabel' => $label);
}
}
return array('found' => false, 'searchLabel' => $label);
}
@cp6
cp6 / form.php
Last active August 7, 2020 14:39
PHP JQUERY AJAX submit form stay on same page example
<?php
if (isset($_POST)) {
$data = '';
foreach ($_POST as $input) {
$data .= $input . ' ';
}
$fp = fopen("formtestoutput.txt", "w");
fwrite($fp, $data);
fclose($fp);
}
@cp6
cp6 / randomtime_func.php
Created July 25, 2020 06:41
PHP generate random time from timestamp
<?php
function randomTimeFormatted(string $max_time): string
{
$t = explode(':', $max_time);
return sprintf("%02d:%02d:%02d", rand(0, $t[0]), rand(0, $t[1]), rand(0, $t[2]));
}
echo randomTimeFormatted('02:38:55');
//02:38:55 will make random string from 0->2 hours, 0->38 mins, 0->55 seconds
@cp6
cp6 / sitemapxml_class.php
Last active July 19, 2020 04:02
PHP sitemap xml builder class
<?php
class sitemapBuilder
{
private function db_connect(): object
{
$host = '127.0.0.1';
$db_name = 'sitemap';
$db_user = 'root';
$db_password = '';
@cp6
cp6 / size_convert_func.php
Created July 4, 2020 04:00
PHP size conversion function
<?php
function convertSize(float $size, string $convert_from = 'B', string $convert_to = 'KB', bool $format = true, int $decimals = 2): float
{
$size = sprintf("%.2f", $size);//Int to float
if ($convert_from === 'B') {
if ($convert_to === 'KB') {
$value = ($size / 1024);
} elseif ($convert_to === 'MB') {
$value = ($size / 1048576);
} elseif ($convert_to === 'GB') {