talmik Posted July 10, 2018 Share Posted July 10, 2018 Hello all This bit of code is supposed to count the number of years between a creation date and the current date then place that number of years in the database. I seem to be missing something or have done something wrong. Can anyone see where it all went wrong? Quote function GetMemberYears($P_CharacterID) { $Creation = SQL_ReadField("Characters",$P_CharacterID,"CreationDate"); $CreationExpand = explode("/",$Creation); $curMonth = date("m"); $Years = date("y") - $CreationExpand[2]; if($curMonth<$CreationExpand[1] || ($curMonth==$CreationExpand[1] && date("d")<$CreationExpand[0])) $Years--; return $Years; } Quote Link to comment https://forums.phpfreaks.com/topic/307478-code-for-year-count/ Share on other sites More sharing options...
Barand Posted July 10, 2018 Share Posted July 10, 2018 Do it the easy way $theDate = '10/08/2005'; $dt1 = DateTime::createFromFormat('d/m/Y', $theDate); $years = $dt1->diff(new dateTime())->y; echo $years; //--> 12 Quote Link to comment https://forums.phpfreaks.com/topic/307478-code-for-year-count/#findComment-1559538 Share on other sites More sharing options...
requinix Posted July 10, 2018 Share Posted July 10, 2018 I sure hope you're not 18 years late to the Y2K party. Quote Link to comment https://forums.phpfreaks.com/topic/307478-code-for-year-count/#findComment-1559539 Share on other sites More sharing options...
Barand Posted July 10, 2018 Share Posted July 10, 2018 I wonder if there will be the same amount of panic when the Unix Epoch ends in the 2030's Quote Link to comment https://forums.phpfreaks.com/topic/307478-code-for-year-count/#findComment-1559541 Share on other sites More sharing options...
requinix Posted July 10, 2018 Share Posted July 10, 2018 Y2K38? Surely everything important is on 64-bit (or higher?) computers by then... Quote Link to comment https://forums.phpfreaks.com/topic/307478-code-for-year-count/#findComment-1559542 Share on other sites More sharing options...
dodgeitorelse3 Posted July 10, 2018 Share Posted July 10, 2018 I have 64 bit computer but I failed a y2k38 test returned a date in 1969 lol, guess I'll be finding solution to this. Of course it is using $date = '2040-02-01'; $format = 'l d F Y H:i'; $mydate1 = strtotime($date); echo '<p>', date($format, $mydate1), '</p>'; but when I use $date = '2040-02-01'; $format = 'l j F Y H:i'; $mydate2 = new DateTime($date); echo '<p>', $mydate2->format($format), '</p>'; I get my expected future date Quote Link to comment https://forums.phpfreaks.com/topic/307478-code-for-year-count/#findComment-1559549 Share on other sites More sharing options...
requinix Posted July 11, 2018 Share Posted July 11, 2018 Your computer may be 64-bit but what about your PHP? The first example will work with 64-bit PHP. Quote Link to comment https://forums.phpfreaks.com/topic/307478-code-for-year-count/#findComment-1559551 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.