Category Archives: PHP

A simple way to debug a php application in production mode

How to Get Start & End dates of a Week by given Date

A simple way to debug a php application in production mode

The following function gets start and end date of a week for a given date supplied in valid date string format. In the following function $week is the number of week in the year. For example the current week is week number 43. In the following function $year is year in YYYY format. For example, current year which is 2016. Here is the function to get start date and end date by a given date. For example
_getWeekStartEndDatesByDate(‘2016-10-26’)

function _getWeekStartEndDatesByDate($current_date) {
$week = date('W', strtotime($current_date));
$year = date('Y', strtotime($current_date));
return _getWeekStartEndDatesByWeekAndYear($week, $year);
}

function _getWeekStartEndDatesByWeekAndYear($week, $year) {
$dto = new DateTime();
$result['start'] = $dto->setISODate($year, $week, 0)->format('Y-m-d');
$result['end'] = $dto->setISODate($year, $week, 6)->format('Y-m-d');
return $result;
}

I hope someone finds it helpful.

A simple way to debug a php application in production mode

Useful PHP debug functions

A simple way to debug a php application in production mode

Here are a few useful PHP debug functions.

I wrote a similar post sometime ago posting the abbreviated “pr” function as a shorthand replacement for the “prinr_r” or “var_dump” functions. Since then this function has changed a bit so re-posting it here with the improvements. With the new updated function one can restrict the output to screen for specific IP address only. Continue reading

A simple way to debug a php application in production mode

How to get file extension using PHP

A simple way to debug a php application in production mode

A simple yet powerful approach to get file extension using PHP. We usually need to extract file extension from file name while uploading file so that we could generate new name and rename file to our convenience. We could create new file name using time, random or hashing, example of which has been given at the last paragraph of this post.

function get_file_extension($filename) {
    return substr($filename, strrpos($filename, '.')+1, strlen($filename)-strrpos('.', $filename));
}

Explanation:

It uses built-in substring, strrpos and strlen functions to extract file extension. A brief on these functions. Continue reading

A simple way to debug a php application in production mode

Useful php debug function to output data with line number

A simple way to debug a php application in production mode

A very useful php debug function i like to use to print debug output to browser window. In some cases where i may not output the debug to browser window i use the same php debug function to receive debug output in my email inbox.

We usually use print_r or var_dump to show debug output. This php debug function uses print_r by default but one can use var_dump to show the output. Continue reading