basil basil - 4 months ago 17
PHP Question

Calculate months and years from given value in PHP, CodeIgniter

Calculate months and years from given value in PHP framework

CodeIgniter


EMI calculator:

enter image description here

Here I have a total months duration, but I need to find out when is come that month and year in CodeIgniter or PHP format.

Answer

You can use the DateTime object for that (please note the missing "," in the datetime constructor).

$datetime1 = new DateTime('2 Jan 2008');// given date and yer in php
$datetime2 = new DateTime('5 July 2012');// current date
$interval = $datetime1->diff($datetime2);// you can find difference and get total month
echo $interval->format('%y years %m months and %d days');

Example:

$date = date('Y-m-d', strtotime('+30 days'));