Skip to main content

Count number of month from date to date

If you have two date and want to count number of month between them, following is function to count this.


public function countMonthFromDate($date){    
    $date1 = $date;    
    $date2 = date('Y-m-d');
    $ts1 = strtotime($date1);
    $ts2 = strtotime($date2);
    $year1 = date('Y', $ts1);
    $year2 = date('Y', $ts2);
    $month1 = date('m', $ts1);
    $month2 = date('m', $ts2);
    $diff = (($year2 - $year1) * 12) + ($month2 - $month1);    
     return $diff;
}

Comments

Popular posts from this blog

Sample VS code setting

  When you are a software developer, you need to configure your IDE working with most convinience for your working, bellow is a sample code snippet to config your IDE {   "diffEditor.ignoreTrimWhitespace" : false ,   "javascript.updateImportsOnFileMove.enabled" : "always" ,   "[typescriptreact]" : {       "editor.defaultFormatter" : "esbenp.prettier-vscode"   },   "editor.formatOnPaste" : true ,   "workbench.settings.applyToAllProfiles" : [],   "editor.tabSize" : 2 ,   "redhat.telemetry.enabled" : true ,   "editor.codeActionsOnSave" : {       },   // "editor.codeActionsOnSave": {   //   "source.fixAll": "explicit",   //   "source.fixAll.eslint": "explicit",   //   "source.organizeImports": "explicit",   //   "source.sortMembers": "explicit",   //   "javascript.showUnused": "...

Docker Compose: Node.js Express and MongoDB example

  Docker   provides lightweight containers to run services in isolation from our infrastructure so we can deliver software quickly. In this tutorial, I will show you how to dockerize Nodejs Express and MongoDB example using   Docker Compose .