Skip to content

Instantly share code, notes, and snippets.

View alphp's full-sized avatar

Fernando Herrero alphp

View GitHub Profile
@alphp
alphp / ANSI.md
Created June 9, 2023 23:37 — forked from fnky/ANSI.md
ANSI Escape Codes

ANSI Escape Sequences

Standard escape codes are prefixed with Escape:

  • Ctrl-Key: ^[
  • Octal: \033
  • Unicode: \u001b
  • Hexadecimal: \x1B
  • Decimal: 27
@alphp
alphp / php-8.1-strftime.php
Last active January 28, 2022 00:26 — forked from bohwaz/php-8.1-strftime.php
strftime() replacement function for PHP 8.1
<?php
namespace PHP81_BC;
/**
* Locale-formatted strftime using IntlDateFormatter (PHP 8.1 compatible)
* This provides a cross-platform alternative to strftime() for when it will be removed from PHP.
* Note that output can be slightly different between libc sprintf and this function as it is using ICU.
*
* Usage:
* use function \PHP81_BC\strftime;
@alphp
alphp / ServiceShell.php
Created March 5, 2019 20:29
CakePHP 3 service example
<?php
namespace App\Shell;
if (!extension_loaded('win32service')) dl('php_win32service.dll');
use Cake\Core\Configure;
use Cake\Console\Shell;
use Cake\Log\Log;
Log::drop('debug');
<?php
ini_set('date.timezone', 'Europe/Madrid');
define('HT', "\x09"); // \x09 \t Horizontal Tab
define('LF', "\x0A"); // \x0A \n Line feed
// Calculamos el inicio del DST: último domingo de marzo (2015-03-29 00:00:00 para 2015).
$date10 = strtotime('last sunday of march');
echo strftime('$date10 = strtotime("last sunday of march"); => %Y-%m-%d %H:%M:%S', $date10), LF;
// Añadimos un día a $date10 de la manera correcta (2015-03-30 00:00:00 para 2015).
@alphp
alphp / DDS238-2 ZN-S Modbus.md
Last active March 24, 2024 21:56
Hiking DDS238-2 ZN/S energy meter
@alphp
alphp / Cake Composer.md
Last active June 5, 2020 14:56 — forked from ogrrd/Cake Composer.md
Install Cake 2.x with Composer

Install CakePHP 2.x with Composer

Windows version

Create project folder

mkdir C:\path\to\your\project
cd C:\path\to\your\project

Create composer.json

@alphp
alphp / ntp.php
Created January 9, 2019 21:45
Query NTP time in PHP
<?php
function ntp_time ($host) {
$msg = hex2bin('e30004fa000100000001000000000000000000000000000000000000000000000000000000000000');
$data = false;
if ($sock = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP)) {
if (@socket_connect($sock, $host, 123)) {
list($xmt['time2'], $xmt['time1']) = explode(' ', microtime());
$xmt['time1'] += 2208988800;
$xmt['time2'] *= 4294967296;
$msg .= pack('N*', $xmt['time1'], $xmt['time2']);
@alphp
alphp / nvram-dump.php
Created August 28, 2018 10:47
DD-WRT nvram dump
<?php
class nvram {
public static function dump (string $nvramfile) {
$nvram = file_get_contents($nvramfile);
$header = unpack('a6header/v1record_count', $nvram);
if ($header['header'] !== 'DD-WRT') {
die('nvram format is not compatible);
}
$record_count = $header['record_count'];
@alphp
alphp / NativeMethods.psm1
Created August 27, 2018 04:08
PowerShell module for send WM_SETTINGCHANGE after change Environment variables in Windows
Add-Type -Namespace Win32 -Name NativeMethods -MemberDefinition @"
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
public static extern IntPtr SendMessageTimeout(IntPtr hWnd, uint Msg, UIntPtr wParam, string lParam, uint fuFlags, uint uTimeout, out UIntPtr lpdwResult);
"@
function Send-SettingChange {
$HWND_BROADCAST = [IntPtr] 0xffff;
$WM_SETTINGCHANGE = 0x1a;
$result = [UIntPtr]::Zero
@alphp
alphp / Environment.psm1
Created August 27, 2018 04:05
PowerShell module for Get/Set/Remove environment variables in Windows
function Get-Environment {
param (
[parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()]$Name,
[String] $DefaultValue = $null,
[ValidateSet("None", "DoNotExpandEnvironmentNames")] [String] $Options = "None"
)
([Microsoft.Win32.Registry]::LocalMachine.OpenSubKey("SYSTEM\CurrentControlSet\Control\Session Manager\Environment", $true)).GetValue($Name, $DefaultValue, $Options)
}