1
  • there is one scenario. assume that:

  • financial year = 01/04/2017 to 31/03/2018

  • I have one input field with datepicker and date column in database. If any user will select today's date e.g 08/06/2017

  • then there should display data for this current year i.e from 01/04/2017 to 08/06/2017 with the help of date table

  • and at the same it should display previous year's data i.e from 01/04/2016 to 31/03/2017
  • For the time I am using $currentdate = date('d/m/Y') to display whole data from db till current date.

$A1 = mysqli_query($conn,'SELECT sum(netvalue) FROMfintranWHERE groupNo = 21 AND docDate <= "'.$currentdate.'" ');

We can create some formula but i am so poor to build logic so please help me

1
  • Possible duplicate of Date minus 1 year? Commented Jun 7, 2017 at 19:02

2 Answers 2

1

You use Carbon.
Documentation :- http://carbon.nesbot.com/docs/

$date = Carbon\Carbon::createFromFormat('d/m/Y', $date);
$date->subYear(); 
echo $date->format('d/m/Y'); // Previous year
Sign up to request clarification or add additional context in comments.

Comments

1

You can use the following method for generating date one year back:

function get_year_back_date($date, $format='d/m/Y') {
    return date($format, strtotime($date . ' -1 year'));
}

$previous_year = get_year_back_date('01/04/2017');

Here you can see this code in action: http://ideone.com/eXRoYB.

3 Comments

May I suggest: date('d/m/Y', strtotime('01/04/'. Date("Y") .' -1 year')) to make it more future safe?
Or whatever date year the user selects
@Andreas I've updated my answer to the fairly general function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.