cooks Posted August 29, 2014 Share Posted August 29, 2014 hi there i am a beginner in PHP and i really would like some help with this..... i need to make use of the date() function to retrieve the current date. Use the split() function to retrieve the day month and the year from the current date. and the calculations to display the age. if anyone could help me with this it would be amazing. thank you!! newagecalc.php Quote Link to comment https://forums.phpfreaks.com/topic/290728-i-am-a-beginner-and-i-really-need-some-help/ Share on other sites More sharing options...
NiallAA Posted August 29, 2014 Share Posted August 29, 2014 (edited) print(Date("F jS Y")); will display: August 29th 2014 http://php.net/manual/en/function.date.php Edited August 29, 2014 by NiallAA Quote Link to comment https://forums.phpfreaks.com/topic/290728-i-am-a-beginner-and-i-really-need-some-help/#findComment-1489263 Share on other sites More sharing options...
cyberRobot Posted August 29, 2014 Share Posted August 29, 2014 (edited) You could format the output from the date() function so it contains some sort of separator. Then just use explode() to break apart the date. More information about both functions can be found in the PHP manual: date() - http://php.net/manual/en/function.date.php explode() - http://php.net/manual/en/function.explode.php Side note: the split() function has been deprecated. Edited August 29, 2014 by cyberRobot Quote Link to comment https://forums.phpfreaks.com/topic/290728-i-am-a-beginner-and-i-really-need-some-help/#findComment-1489264 Share on other sites More sharing options...
Strider64 Posted August 29, 2014 Share Posted August 29, 2014 Or you could just do this <?php $born = new DateTime("August 28, 1964"); $now = new DateTime(); echo $now->format("F j, Y"); // Output August 29, 2014 format: echo '<br>'; $age = $born->diff($now); echo "I am " . $age->y . " years old<br>"; More info on DateTime() here: http://php.net/manual/en/class.datetime.php Quote Link to comment https://forums.phpfreaks.com/topic/290728-i-am-a-beginner-and-i-really-need-some-help/#findComment-1489266 Share on other sites More sharing options...
mac_gyver Posted August 29, 2014 Share Posted August 29, 2014 ^^^ except that the stated age of something depends on if the birthday has or has not occurred yet in the current year. the age is the difference in years, subtract one if the month and day are less than the current month and day. Quote Link to comment https://forums.phpfreaks.com/topic/290728-i-am-a-beginner-and-i-really-need-some-help/#findComment-1489267 Share on other sites More sharing options...
Strider64 Posted August 29, 2014 Share Posted August 29, 2014 Maybe something like? $calendarDay = new DateTime( $month . ' ' . $day . ', ' . $year ); $age = $born->diff($calendarDay); I just wrote first post on how easy it is to do it this way and the learning part is actually figuring it out on your own (in my opinion). Quote Link to comment https://forums.phpfreaks.com/topic/290728-i-am-a-beginner-and-i-really-need-some-help/#findComment-1489270 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.