Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get last year's start and end date?

Tags:

date

php

How can I get last year's start and end date using PHP code? Is it possible?

like image 349
Kichu Avatar asked Mar 17 '12 06:03

Kichu


2 Answers

The first day is always January 1, the last day is always December 31. You're really only changing the year attached to it. Depending on how you want the date formatted, you have a couple possibilities...

  1. If you just want to display the physical date:

    $year = date('Y') - 1; // Get current year and subtract 1
    $start = "January 1st, {$year}";
    $end = "December 31st, {$year}";
    
  2. If you need the timestamp for both those dates:

    $year = date('Y') - 1; // Get current year and subtract 1
    $start = mktime(0, 0, 0, 1, 1, $year);
    $end = mktime(0, 0, 0, 12, 31, $year);
    

Very simple stuff. You can manually specify which year if you wanted too. The premise is the same.

like image 173
animuson Avatar answered Nov 18 '22 15:11

animuson


You can do it by using the below. Hope it helps someone.

//to get start date of previous year
echo date("d-m-y",strtotime("last year January 1st"));

//to get end date of previous year
echo date("d-m-y",strtotime("last year December 31st"));
like image 11
PraveenKumar Avatar answered Nov 18 '22 15:11

PraveenKumar