kb5220 Posted June 10, 2011 Share Posted June 10, 2011 Hi developer. I am writing here because I am shock into a problem. I am starting to make a page that runs much of dates. Example. I sign on page and then I will write a date from the birthday. let and say I would write my birthday as 08-02-1991 then it could figure out when I once again have a birthday / one week before. Thanks in advance for your help. Quote Link to comment https://forums.phpfreaks.com/topic/238993-how-can-i-get-next-year-by-birthday/ Share on other sites More sharing options...
wildteen88 Posted June 10, 2011 Share Posted June 10, 2011 You can use strtotime. An example script // set the date of birth $dob = '08-02-1991'; // get the timestamp for the dob $dobTime = strtotime($dob); // get the timestamp 1 week before the dob timestamp $dob1WeekBefore = strtotime('-1 Week', $dobTime); echo 'Birthday:<br />' . date('D jS M Y', $dobTime) . '<br /><br />'; echo 'Week Before Birthday:<br />' . date('D jS M Y', $dob1WeekBefore); Note. strtotime will not work with any dates before 1st of Jan 1970. Quote Link to comment https://forums.phpfreaks.com/topic/238993-how-can-i-get-next-year-by-birthday/#findComment-1228010 Share on other sites More sharing options...
kb5220 Posted June 10, 2011 Author Share Posted June 10, 2011 Thanks for you help. But what i think was my birthday was 08-02-1991, so will i have it to figure out when the next dato was 08-02-2012 and one week before that Hope you understand Quote Link to comment https://forums.phpfreaks.com/topic/238993-how-can-i-get-next-year-by-birthday/#findComment-1228026 Share on other sites More sharing options...
wildteen88 Posted June 10, 2011 Share Posted June 10, 2011 You'll need to calculate the difference in years (current year - dob year) // get the dob year list(,,$dobyear) = explode('-', $dob); // get the difference in years // add 1 to get the date for next years birthday $years = (date('Y') - $dobyear) + 1; date('Y') returns the current year. We'll use this code to get the date a week before the birthday next year $dob1WeekBefore = strtotime("+$years year -1 Week", $dobTime); Full code $dob = '08-02-1991'; $dobTime = strtotime($dob); list(,,$dobyear) = explode('-', $dob); $years = (date('Y') - $dobyear) + 1; $dob1WeekBefore = strtotime("+$years year -1 Week", $dobTime); echo 'You where born on:<br />' . date('D jS M Y', $dobTime) . '<br /><br />'; echo 'Week before your birthday:<br />' . date('D jS M Y', $dob1WeekBefore); Quote Link to comment https://forums.phpfreaks.com/topic/238993-how-can-i-get-next-year-by-birthday/#findComment-1228062 Share on other sites More sharing options...
kb5220 Posted June 11, 2011 Author Share Posted June 11, 2011 Thanks it was really helpful. Now i made this <?php $day = "12"; $month = "06"; $year = "1991"; if (date("d") <= $day && date("m") <= $month){ echo date("d-m-Y", mktime(0,0,0,$month,$day - 7,date("Y"))); }else { $dob = "$day-$month-$year"; $dobTime = strtotime($dob); list(,,$dobyear) = explode('-', $dob); $years = (date('Y') - $dobyear) + 1; $dob1WeekBefore = strtotime("+$years year -1 Week", $dobTime); echo date('d-m-Y', $dob1WeekBefore); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/238993-how-can-i-get-next-year-by-birthday/#findComment-1228278 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.