Skip to main content

Posts

Find the dates ( month start and end dates) between the given two dates using PHP

When you want to pull the dates between two dates, where the between dates are connecting dots. If you want to list the complete date ranges through months. For example, to find the dates between below dates date1 = '2022-12-29'; date2 = '2023-02-20'; expected output would be  Start Date End Date 2022-12-29 2022-12-31 2023-01-01 2023-01-31 2023-02-01 2023-02-20 <?php /* * Returns array of month's start & end dates between provided dates. * * @param string $start_date * @param string $end_date * * * @return array */ function getDates($start_date, $end_date){ $start_date_obj = new DateTime($start_date); $end_date_obj = new DateTime($end_date); $diff = $end_date_obj->diff($start_date_obj); $months = (($diff->y) * 12) + ($diff->m); $dates[] = array('start_date' => $start_date, 'end_date' => date('Y-m-t', strtotime($start_date)), 'suffix' =

Sorting an array of objects using Javascript

 When working on one of my projects, I had to sort the elements of the JSON array of objects. I did this most of the time from server side but this time I had to do the client side sort. Here is the solution what I found. We've two functions one to sort by ascending and other for descending. It expects first parameter as array (of objects) and second parameter the key on basis of which we want to sort. <script> function sortByKeyDesc(array, key) { return array.sort(function (a, b) { var x = a[key]; var y = b[key]; return ((x > y) ? -1 : ((x < y) ? 1 : 0)); }); } function sortByKeyAsc(array, key) { return array.sort(function (a, b) { var x = a[key]; var y = b[key]; return ((x < y) ? -1 : ((x > y) ? 1 : 0)); }); } </script> Here is the example <script> books = [{ 'BookID': 123, 'BookName': 'Seven Habits of most effective people' }, { 'BookID': 234, 'BookName': 'W

Multiple Checkboxes Validation

This is a regular problem and solution is hardly available on google. When we use multiple checkboxes of same group (same name with square brackets) and you've to read the values from server side, then below solution will help you. Here I'm using jQuery (https://jquery.com/) and jQuery Validate plugin (https://jqueryvalidation.org/) For an example, I've to ask a user which of the listed book they are interested to know about <form id="BooksForm" method="post" name="BooksForm"> <p>Books you are interested in </p> <input class="Books" id="Book1" name="Books[]" type="checkbox" value="1" /> The Inner Game of Tennis <br /> <input class="Books" id="Book2" name="Books[]" type="checkbox" value="1" /> Monk who sold his ferrari <br /> <input class="Books" id="Book3" name=&quo

PHP Code Review Guidelines

General  The code works  The code is easy to understand  Follows coding conventions  Names are simple and if possible short  Names are spelt correctly  Names contain units where applicable  There are no usages of magic numbers  No hard coded constants that could possibly change in the future  All variables are in the smallest scope possible  There is no commented out code  There is no dead code (inaccessible at Runtime)  No code that can be replaced with library functions  Variables are not accidentally used with null values  Variables are immutable where possible  Code is not repeated or duplicated  There is an else block for every if clause even if it is empty  No complex/long boolean expressions  No negatively named boolean variables  No empty blocks of code  Ideal data structures are used  Constructors do not accept null/none values  Catch clauses are fine grained and catch specific exceptions  Exceptions are not eaten if caught, unless explicitly documente

Week dates between two dates

To list week's start date & end date between given two dates. It also includes number of dates in every set. It allows you to list only those weeks having total seven days. Here the starting day of the week is Monday & the end day of the week is Sunday. /* * Returns array of week's start & end dates with number of days between those. * * @param string $start_date * @param string $end_date * @param boolean $only_full_week * * @return array */ function getWeekDates($start_date, $end_date, $only_full_week = false) { $stime = strtotime($start_date); $etime = strtotime($end_date); $weeks = array(); $i = 0; $j = 1; while ($stime <= $etime) { if ($i == 0 && $j == 1) { $weeks[$i]['start_date'] = date('Y-m-d', $stime); $weeks[$i]['end_date'] = date('Y-m-d', $stime); $weeks[$

Format Sentences, format number and remove extra spaces. All in one solution

We had a requirement, where we were asked to format the content from a big database table. And the format includes making sentences to sentence case, format simple numbers to comma separated numbers within those sentences and remove the extra spaces. Here is the solution. Hope this helps you. function formatString($string){ //number formatting $string1 = preg_replace_callback('/\d+/', function($match){return number_format($match[0]);}, $string); //removing extra spaces $string2 = preg_replace('/\s+/', ' ', $string1); //sentence case $sentences = preg_split('/([.?!]+)/', $string2, -1, PREG_SPLIT_NO_EMPTY|PREG_SPLIT_DELIM_CAPTURE); $new_string = ''; foreach ($sentences as $key => $sentence) { $new_string .= ($key & 1) == 0? ucfirst(strtolower(trim($sentence))) : $sentence.' '; } return trim($new_string); } $str = "it was an awesome day. i bought shares f

Huge data Excel download using PHP

Ah! It was always a problem to download huge data in excel format. Tried the best library available 'PHPExcel' but the issue persists with memory limit and time taken to generate and download. But here is the promising solution found after spending ages searching on Google. You can find it here . The author himself says "Never run out of memory with PHPExcel again". You need to download it and include  xlsxwriter.class.php   where you want to fetch the data from DB and through it on to browser or save to a particular location. Let's get started. Below is the logic to generate the file and store in the current directory. If you want to store is at the desired location, specify the location for the method writeToFile. include_once("xlsxwriter.class.php"); $writer = new XLSXWriter(); $data = array( array('year','month','amount'), array('2003','1','220'), array('2003','2'