PHP Scripts Collection

Here you can find a collection of ready PHP Scripts that you can use within the extensions as well as anywhere you desire.

How to check if current day of week is Saturday or Sunday

Below you can find a PHP snippet that will allow you to check whether the current day of the week is a weekday.

// Method that checks whether the current day of week is either Saturday or Sunday using Joomla API
function isWeekend()
{
    // Comma separated list of days to check. Sunday = 7
    $allowDays = array(6,7);

    // Do not edit below
    return in_array(JFactory::getDate()->format('N'), $allowDays);
}

// Call method
if (isWeekend())
{
    echo 'Hooray! is weekend!';
}

How to check if a user has certain cookies stored in their browser

Below you can find a PHP snippet that will allow you to check if a user has certain cookies stored in their browser.

// Method that checks if multiple cookies exist using Joomla API
function cookiesExist($cookies)
{
    foreach ($cookies as $cookie)
    {
        if (!$pass = (bool) JFactory::getApplication()->input->cookie->get($cookie))
        {
            return false;
        }
    }

    return true;
}

// The cookie names to check
$cookies = [
    'some_cookie_a', 
    'some_cookie_b', 
    'some_cookie_c'
];

// Check cookies
if (cookiesExist($cookies))
{
    echo 'All cookies exist!';
}
Last updated on Nov 28th 2024 18:11