sábado, 31 de diciembre de 2022

change time zone and set time zone to UTC

 <?php

$date = new DateTime();


$date->setTimezone(new DateTimeZone('America/Los_Angeles'));


echo $fdate = $date->format('Y-m-d H:i:s');


echo date('Y-m-d H:i:s T', time()) . "<br>\n";



date_default_timezone_set('UTC');

echo date('Y-m-d H:i:s T', time()) . "<br>\n";


?>

Calling PHP functions within HEREDOC strings

 echo <<<EOT

One month ago was ${!${''} = date('Y-m-d H:i:s', strtotime('-1 month'))}.
EOT;

https://stackoverflow.com/questions/104516/calling-php-functions-within-heredoc-strings

lunes, 26 de diciembre de 2022

jueves, 22 de diciembre de 2022

print the max value of an array

 <?php


$array = array(1, "hello", 1, "world", "hello",1,1,1,1);

print_r(max(array_count_values($array)));

?>


this first count the values, then  print the max value, Will print 6, as there are 6 1

?>

miércoles, 7 de diciembre de 2022

compare array using key and a call back function

 <?php

function key_compare_func($key1, $key2)

{

    if ($key1 != $key2)

        return 1;

    

}


$array1 = array('blue'  => 1, 'red'  => 2, 'green'  => 3, 'purple' => 4);

$array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan'   => 8);


print_r(array_diff_ukey($array1, $array2, 'key_compare_func'));


---------------------------------------------------

Array ( [red] => 2 [purple] => 4 )

compare an array using call back function

 <?php


function key_compare_func($a, $b)

{

    if ($a === $b) {

        return 0;

    }

}


$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");

$array2 = array("a" => "green", "yellow", "red");

$result = array_diff_uassoc($array1, $array2, "key_compare_func");

print_r($result);




echo "<br><br>";


function key_compare_func1($a, $b)

{

    if ($a === $b) {

        return 1;

    }

}


$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");

$array2 = array("a" => "green", "yellow", "red");

$result = array_diff_uassoc($array1, $array2, "key_compare_func1");

print_r($result);



Depending on the return value   of the functions the output  will be inverted