Showing posts with label date function. Show all posts
Showing posts with label date function. Show all posts

Monday, November 21, 2016

Calculate age from date of birth php

Most easiest way to find age (years/month)

 function return_age($dob){  
   $from = new DateTime($dob);  
   $to  = new DateTime('today');  
   if( $from->diff($to)->y!=0){  
        echo $from->diff($to)->y." Years";  
   }  
   else{  
     echo $from->diff($to)->m." Months";  
   }  
 }  

now use it echo return_age('2010-02-01');

if  age less than one year then it'll show month like 9 Months.

Monday, June 27, 2016

Get / display all dates between two dates-php

It's very common uses to get all dates between two dates rang.

However php has a own class callled "DatePeriod" class which made our work easy for this task.


 $begin = new DateTime('2016-06-01');  
 $end = new DateTime('2016-06-28');  
 $end = $end->modify( '+1 day' );   
 $daterange = new DatePeriod($begin, new DateInterval('P1D'), $end);  
 foreach($daterange as $date){  
   echo $date->format("Y-m-d") . "<br>";  
 }  

Note :
 //(P1D stands for period of one day, see DateInterval for further documentation)
 //$end = $end->modify( '+1 day' ); mean dates include the last day 2016-06-28  

Output will looks like
2016-06-01
2016-06-02
2016-06-03
2016-06-04
2016-06-05

Total Pageviews