codeigniter - DatedIF equivalent in PHP -
need find function in php return me number of years ,months , dates between 2 given dates
for eg: datedif("20-05-2015","20-05-2015","m") return 1 month in excel while using date_diff 31 days .
the correct way work dates in php >= 5.2.0 using datetime, i.e.:
$startraw = '2013-05-10 12:04:58'; $start = datetime::createfromformat('y-m-d h:i:s', $startraw ); $endraw = date("2015-05-10 12:04:58"); $end = datetime::createfromformat('y-m-d h:i:s', $endraw ); $diff = $start->diff($end); echo 'difference: ' . $diff->format('%y years') . "\n";
output:
difference: 2 years
demo:
format
the following characters recognized in format parameter string. each format character must prefixed percent sign (%).
% literal % % y years, numeric, @ least 2 digits leading 0 01, 03 y years, numeric 1, 3 m months, numeric, @ least 2 digits leading 0 01, 03, 12 m months, numeric 1, 3, 12 d days, numeric, @ least 2 digits leading 0 01, 03, 31 d days, numeric 1, 3, 31 total amount of days 4, 18, 8123 h hours, numeric, @ least 2 digits leading 0 01, 03, 23 h hours, numeric 1, 3, 23 minutes, numeric, @ least 2 digits leading 0 01, 03, 59 minutes, numeric 1, 3, 59 s seconds, numeric, @ least 2 digits leading 0 01, 03, 57 s seconds, numeric 1, 3, 57 r sign "-" when negative, "+" when positive -, + r sign "-" when negative, empty when positive -,
learn more datetime class
Comments
Post a Comment