1
$a= date("Y");

What will be the data-type of $a: A number or a string? If it's a string, how do i get the previous year in PHP?

1
  • 2
    It is a variable. You can use it as a string. Or just subtract one from it. $last_year=date('Y')-1; Dec 30, 2014 at 5:08

4 Answers 4

5
echo date("Y",strtotime("-1 year"));  //last year "2013"
echo date("Y");                       //current year "2014"

Also follow this post

And this post

6
  • So does, date("Y") gives the current year as a string? Dec 30, 2014 at 5:19
  • @Ashutosh Yes,please use my both example in your php editor and see the result ,instead asking the answer.
    – Priyank
    Dec 30, 2014 at 5:21
  • @AshutoshSaboo:date("Y") give "2014"
    – Priyank
    Dec 30, 2014 at 5:23
  • Actually I have seen these two posts already earlier, and these were the one's that caused me a lot of confusion, because somewhere some have said that date("Y") gives the current year as a string, and somewhere some say that it gives the current year as a number (because somewhere arithmetic operations are performed and somewhere string operations are performed). So, which one is correct: date("Y") gives the current year as a string/number? That's the entire reason why I asked this question, PRECISELY. Dec 30, 2014 at 5:27
  • so date("Y") gives the year as a string right? Can you also help me out on my other question: stackoverflow.com/questions/27692895/… . Thanks! :) Dec 30, 2014 at 5:30
3
  <?php
    echo $a= date("Y")-1;
   ?>

http://codepad.org/d0VjiyS5

its is not either string or number ,we can define it as string or else int using casting in php by using (int)

2
  • So, according to you date("Y") gives the current year "BY DEFAULT" in which data-type (number/string) , or does it give it both as a string and a number, and hence we can perform both arithmetic and string operations on date("Y")? Dec 30, 2014 at 5:24
  • Can you also help me out on my other question: stackoverflow.com/questions/27692895/… . Thanks! :) Dec 30, 2014 at 5:29
1

You can use PHP's relative date/time formats:

date('Y', strtotime('last year'));
2
0
echo date("Y",strtotime("-1 year"));
2
  • @Rizier123: So does, date("Y") give the current year as a string? Dec 30, 2014 at 5:13
  • @Rizier123: Can you also help me out on my other question: stackoverflow.com/questions/27692895/… . Thanks! :) Dec 30, 2014 at 5:29

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.