PHP end a financial year in june of every year

118

I would like to do something of this sort in php, when the month reaches June 2016, I would like to echo 2016-17, but before that i echo 2015-16 What I have so far echos 2015-16 meaning it changes with the beginning of the year, but i would rather it does that in june onwards,

echo   date('Y') .'-'.date('y', strtotime('+1 year'));

Any suggestions

548

Answer

Solution:

Very basic but you can use it:

<?php
if (date('m') > 6) {
    $year = date('Y')."-".(date('Y') +1);
}
else {
    $year = (date('Y')-1)."-".date('Y');
}
echo $year; // 2015-2016
?>

Explanation:

If current month is greater than 6 than it will print current year and +1 Year and hyphen (-) between them.

If current month less than 6 than it will print current year and -1 Year and hyphen (-) between them.

637

Answer

Solution:

This can be solved with a simple oneliner:

echo date('n') > 6 ? date('Y').'-'.(date('Y') + 1) : (date('Y') - 1).'-'.date('Y');

The "n" format character is better in this case, as it returns the month without leading zero.

Be aware, that this solution depends on your machine's timezone settings. If you use this in an international, or at least in many timezones, this might be incorrect.

109

Answer

Solution:

Try this :

if(date('m') >= 06) {
   $d = date('Y-m-d', strtotime('+1 years'));
   echo   date('Y') .'-'.date('y', strtotime($d));
} else {
  $d = date('Y-m-d', strtotime('-1 years'));
  echo   date('Y', strtotime($d)).'-'.date('y');
}

Demo : http://codepad.org/Z1zWIaiD

People are also looking for solutions to the problem: php - Null result while use and operator in MYSQL IN

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.