wright67uk Posted December 9, 2012 Share Posted December 9, 2012 I have a variable; $year = date("Y"); Which I echo on my page; echo $year; which gives the output 2012. Either with text or images, I would like to put a plus symbol and a minus symbol either side of the 2012. If the - symbol is pressed, $year will have the value of 2011 and if the + symbol is press it will have the value of 2013. How would i go about this in the correct manner, and can you provide a basic example? Quote Link to comment https://forums.phpfreaks.com/topic/271777-1-to-year-value/ Share on other sites More sharing options...
Christian F. Posted December 9, 2012 Share Posted December 9, 2012 (edited) I recommend having a look at the DateTime class, or strtotime (). Should give you what you need. Edited December 9, 2012 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/271777-1-to-year-value/#findComment-1398334 Share on other sites More sharing options...
Andy123 Posted December 9, 2012 Share Posted December 9, 2012 (edited) As Christian F. said, the DateTime class would be good for this purpose. For adding a year, take a look at the add function. Begin by creating a DateTime object and set its time and then use the add function where you pass a DateInterval object. See how to instantiate this class here. For substracting a year, the same approach applies, except that the sub function should be used instead of add. You can find more information about the DateTime class by following Christan F.'s link above. If you really want to keep things simple and avoid all of these classes, then you could use a simpler approach that is less pretty and flexible. $year = (int) date("Y"); $next_year = ++$year; $previous_year = --$year; If you want to support adding or subtracting months, for example, in the future, then you will be in trouble and wish you had gone with the DateTime approach. Edited December 9, 2012 by Andy123 Quote Link to comment https://forums.phpfreaks.com/topic/271777-1-to-year-value/#findComment-1398358 Share on other sites More sharing options...
PFMaBiSmAd Posted December 9, 2012 Share Posted December 9, 2012 If you are working with a date that includes the day of the month, you will need to account for leap year when switching years, since every year doesn't have a Feb 29th. If you are just working with the year or the year and month - <?php // get the submitted year or the current year as a default $year = (isset($_GET['year'])) ? (int)$_GET['year'] : date('Y'); // produce 'yearination' links (keeping any other get parameters that might be present) $year_links = ''; // previous link $_GET['year'] = $year-1; // set/replace the pagination GET parameter (all other GET parameters unchanged) $year_links .= "<a href='?" . http_build_query($_GET, '', '&') . "'>-</a> "; // display current year $year_links .= " $year "; // next link $_GET['year'] = $year+1; // set/replace the pagination GET parameter (all other GET parameters unchanged) $year_links .= " <a href='?" . http_build_query($_GET, '', '&') . "'>+</a>"; ?> <style type="text/css"> a:link {text-decoration:none;} </style> <?php // output the links somewhere on your page echo $year_links; Quote Link to comment https://forums.phpfreaks.com/topic/271777-1-to-year-value/#findComment-1398363 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.