View Full Version : Get Year Duration
megha_3000
04-23-2013, 01:59 AM
Hello everybody,
i have 2 dates in my database
dateStart and dateEnd both stored as as type date: y-m-d (eg 2003-05-05)
how can i get years.
Thanking You
Megha
djr33
04-23-2013, 02:46 AM
One option:
http://php.net/manual/en/function.mktime.php
Another would be to use $date = explode('-',$date) on both (with different variable names of course) then just use $date[0] to get the year from each, then subtract. That assumes exactly equivalent formats all of the time.
The first option will allow you a little more precision (even including months and days) and to know how much time or how many calendar years have passed; the second will only allow you to consider calendar years. (So Dec 31-Jan 1 = 1 year; Jan 1-Dec 31 = 0 years. Is that correct?)
check out the DateTime class (http://php.net/datetime) as well.
fastsol1
04-23-2013, 11:24 AM
Or
echo date("Y", strtotime($row['dateStart']));
megha_3000
05-14-2013, 04:32 AM
Resolved Get Year Duration .
Thank you all for your quick feedback. I got a problem to get the total years. After that this is solved.
At first the datediff() function gives the days. And then by dividing it by 365 it returns the years
e.g.
sum(datediff( `Date1` , `Date2` ) ) AS totalDays // this gives total days
sum( datediff( `Date1` , `Date2` ) ) /365 AS totalYears // this gives total years
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.