Jump to content

+1 To Year Value


wright67uk

Recommended Posts

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?

Link to comment
Share on other sites

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 by Andy123
Link to comment
Share on other sites

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;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.