PHP: Convert datetime from one timezone to another

<?php

$datetimestr = $db->get_var("select MYDATETIME from MYTABLE where ID=123");
$datetimestr = '2022-12-30 05:45:00';
$mydate = new DateTime($datetimestr, new DateTimeZone('Europe/London'));
$mydate->setTimezone(new DateTimeZone('America/Los_Angeles'));
$newdatetimestr = $mydate->format('F j, Y');

?>

PHP: Number of Business Days between 2 Dates

function number_of_working_days($from, $to) {
    $workingDays = [1, 2, 3, 4, 5]; # date format = N (1 = Monday, ...)
    $holidayDays = ['*-12-25', '*-01-01', '2013-12-23']; # variable and fixed holidays

    $from = new DateTime($from);
    $to = new DateTime($to);
    $to->modify('+1 day');
    $interval = new DateInterval('P1D');
    $periods = new DatePeriod($from, $interval, $to);

    $days = 0;
    foreach ($periods as $period) {
        if (!in_array($period->format('N'), $workingDays)) continue;
        if (in_array($period->format('Y-m-d'), $holidayDays)) continue;
        if (in_array($period->format('*-m-d'), $holidayDays)) continue;
        $days++;
    }
    return $days;
}

echo number_of_working_days('2013-12-23', '2013-12-29'); // Result: 3

PHP: Echo Immediately (Flush)

If you print a very long webpage, PHP buffer can’t fit into web server RAM. Solution is FLUSH.

Ob_flush Example

$handle = fopen('/var/log/apache2/domains/www.test.net', "r") or die("Couldn't get handle");
if ($handle)
{
  while (!feof($handle))
  {
    $myline = fgets($handle,4096);
    echo $myline."\n";
    ob_flush();
    flush();
  }
  fclose($handle);
}

PHP: Extract Domain Name from URL

function getDomainName($url)
{
  $pieces = parse_url($url);
  if (isset($pieces['host'])) $domain=$pieces['host']; else $domain='';
  if(preg_match('/(?P<domain>[a-z0-9][a-z0-9\-]{1,63}\.[a-z\.]{2,6})$/i', $domain, $regs))
  {
    return $regs['domain'];
  } else
  {
    return '';
  }
}

echo getDomainName("http://example.com").'<br>'; // outputs 'example.com'
echo getDomainName("http://www.example.com").'<br>'; // outputs 'example.com'
echo getDomainName("http://mail.example.co.uk").'<br>'; // outputs 'example.co.uk'
echo getDomainName("https://test1.test2.test3.example.co.uk").'<br>'; // outputs 'example.co.uk'

PHP: Validate Domain Name

function validate_domain_name($domain)
{
  $domain=strtolower($domain);

  if (substr($domain, 0, 7) === 'http://')
  {
    $domain = substr($domain, 7);
  }

  if (substr($domain, 0, 8) === 'https://')
  {
    $domain = substr($domain, 8);
  }

  if (substr($domain, 0, 4) === 'www.')
  {
    $domain = substr($domain, 4);
  }

  if(!substr_count($domain, '.'))
  {
    return '';
  } else
  {
    if (filter_var('http://'.$domain, FILTER_VALIDATE_URL)) return $domain; else return '';
  }
}

if (validate_domain_name($domain)!='')
{
  echo $domain.' is a valid domain name';
}

PHP & MySQL: Emoji Problem

Emoji characters are 4 bytes!

MySQL by default only uses a three byte encoding and so values in the four byte range (eg. Asian characters and Emojis) can not be stored. MySQL does provide full four byte UTF-8 support, but it requires certain database settings to be configured.

‘utf8_unicode_ci’ was the recommended collation. We now recommend using ‘utf8mb4_unicode_ci’ which supports four byte characters (utf8_unicode_ci only supports three).

If you want to save emojis in MySQL/MariaDB database:

  • Database and table’s charset must be: utf8mb4_unicode_ci
  • Connection also needs to be utf8mb4 not utf8 for it to work