Tuesday, January 6, 2015

PHP: The Right Way

There’s a lot of outdated information on the Web that leads new PHP users astray, propagating bad practices and insecure code. PHP: The Right Way is an easy-to-read, quick reference for PHP popular coding standards, links to authoritative tutorials around the Web and what the contributors consider to be best practices at the present time. Know where to find good information!

PHP: The Right Way

Friday, February 15, 2013

Function to get the last day (saturday) of current week


/**
 * Function to get the last day (saturday) of current week
 *
 * @return string
 */
function get_week_end_date()
{
$format = 'M d, Y';

// if today is saturday, return today's date
if (date('N') === 7)
{
return date($format);
}
else
{
// return next saturday's date
return date($format, strtotime('next saturday'));
}
}

Thursday, September 6, 2012

Get Days of the Week

This function can be used to get the days of the week:

function get_week_days()
{
    $timestamp = strtotime('monday this week');
    $week_days = array();
    for ($i = 1; $i <= 7; $i++)
    {
        $day = array();
        $day['name'] = strftime('%A', $timestamp);
        $day['short_name'] = strftime('%a', $timestamp);
        $week_days[$i] = $day;
        $timestamp = strtotime('+1 day', $timestamp);
    }

    return $week_days;
}


Output of this function will be:

Array
(
    [1] => Array
        (
            [name] => Monday
            [short_name] => Mon
        )

    [2] => Array
        (
            [name] => Tuesday
            [short_name] => Tue
        )

    [3] => Array
        (
            [name] => Wednesday
            [short_name] => Wed
        )

    [4] => Array
        (
            [name] => Thursday
            [short_name] => Thu
        )

    [5] => Array
        (
            [name] => Friday
            [short_name] => Fri
        )

    [6] => Array
        (
            [name] => Saturday
            [short_name] => Sat
        )

    [7] => Array
        (
            [name] => Sunday
            [short_name] => Sun
        )

)
 
 
Hope this function will be useful to list the days of the week somewhere in your projects.
Happy coding..:) 

Monday, April 16, 2012

Date Range Checking Using PHP

1)
/**
* Function to check if a date is within a date range
* Returns true if date falls within startDate and startDate
*
* @param timestamp $date
* @param timestamp $startDate
* @param timestamp $startDate
*
* @return boolean
*/
public function isDateInRange($date, $startDate, $endDate)
{
return (($date >= $startDate) && ($date <= $endDate));
}

2)

/**
* Function to check if a date range falls within another date range
*
* @param timestamp $startDate1 first date range startDate
* @param timestamp $endDate1 first date range endDate
* @param timestamp $startDate2 second date range startDate
* @param timestamp $endDate2 second date range endDate
*
* @return boolean
*/
public function isDateRangeInDateRange($startDate1, $endDate1, $startDate2, $endDate2)
{
return ($this->isDateInRange($startDate2, $startDate1, $endDate1) ||
$this->isDateInRange($endDate2, $startDate1, $endDate1));
}

Friday, March 16, 2012

Get First and Last Date of the Current Week - The MySql Way

First Day of the Week

SELECT DATE_ADD(current_date, INTERVAL(1-DAYOFWEEK(current_date)) DAY) FIRST_DAY_OF_WEEK;

Last Day of the Week

SELECT DATE_ADD(current_date, INTERVAL(7-DAYOFWEEK(current_date)) DAY) LAST_DAY_OF_WEEK;