3 May 2011

How to get current, last and coming year in PHP. Simple examples of how you can get different years. Currently the years are calculated from the server time.

Source code viewer
  1. // Set your desired timezone, if server default is not for you. Calculating years the error can be just a day before and after new year.
  2. date_default_timezone_set('Europe/Tallinn');
  3.  
  4. // Get current year.
  5. date('Y', strtotime('now'));
  6. // Or just as default time is "now"
  7. date('Y');
  8.  
  9. // Last year.
  10. date('Y', strtotime('-1 year'));
  11.  
  12. // Next year.
  13. date('Y', strtotime('+1 year'));
  14.  
  15. // 100 years from today.
  16. date('Y', strtotime('+100 years'));
Programming Language: PHP