Tuesday, September 2, 2014

Timestamp creation using PHP maintaining the timezone

Time and date manipulation would appear in most PHP applications. PHP provides a nice method to create timestamp using strtotime. The PHP documentation gives the types of arguments we could pass in strtotime to get the timestamp.

For creating the current timestamp we could use something like below

strtotime(date('Y-m-d H:i:s'));

Please note that date function in php works based on the timezone settings for the application set in the application. We could also set the timezone using the method date-default-timezone-set.

The time conversion and display would be tricky sometimes. It would be better that we store the date time in GMT in database so that even when we change the timezone, it would work fine and display the correct time as conversion from GMT to the existing timezone is easier.

When creating a timestamp value from a GMT date time value using strtotime, we should append the date time value with "+00:00". This would create the correct timestamp related to the GMT. 

Thursday, August 21, 2014

PHP Online compiler

Whenever you need to test something quickly in php we would need to create a test file and write the code and run in the browser and see the result.
This could be simplified if we use any online php compiler so that we dont have to go through all those hassles.
I mostly use the website Compileonline.com in order to write a php code to check something.
There might be other online compilers like this but I liked this one very much and so was using it very often and thought about sharing.
The website also provides compiling in another languages also. Do check it out!

Wednesday, August 20, 2014

PHP multi dimensional array sorting utility function

PHP has a great collection of array functions. When it comes to sorting a multi dimensional array we have the array_multisort function in PHP.
This particular function helps in sorting multiple arrays as well as sorting a multi dimensional array. Please check the documentation here
When using this function to sort a multi dimensional array, we need to do some manipulation in the array before passing it to the array_multisort function. I have created a utility function which does this manipulation. Please feel free to use this function
1:  function getMultiSortedArray($multiDimenAry, $sortKeyOne, $sortKeyOneOrder = SORT_ASC, $sortKeyTwo = '', $sortKeyTwoOrder = SORT_ASC){  
2:      foreach ($multiDimenAry as $key=>$row){  
3:          $tempOneArry[$key] = $row[$sortKeyOne];  
4:          if( !empty($sortKeyTwo) ){  
5:              $tempTwoAry[$key] = $row[$sortKeyTwo];  
6:          }  
7:      }  
8:      if( !empty($sortKeyTwo) ){  
9:          array_multisort($tempOneArry, $sortKeyOneOrder,$tempTwoAry, $sortKeyTwoOrder, $multiDimenAry);  
10:      }else{  
11:          array_multisort($tempOneArry, $sortKeyOneOrder, $multiDimenAry);  
12:      }  
13:      return $multiDimenAry;  
14:  }  

I would update with a pastebin link with the usage of this function.
Please click here for the usage of the function