Home » Php » php – How do I get last year's start and end date?

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

Posted by: admin July 12, 2020 Leave a comment

Questions:

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

How to&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.

Answer:

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"));

Answer:

start date of the year :

mktime(0,0,0,1,1,$year);

end date of the year :

mktime(0,0,0,1,0,$year+1);

Answer:

Check this Stuff

$currentY = date('Y');
$lastyearS = mktime(0, 0, 0, 1, 1,  $currentY-1 )."<br/>";
$lastyearE = mktime(0, 0, 0, 12, 31,  $currentY-1 )."<br/>";
echo date('Y-m-d',$lastyearS)."<br/>";echo date('Y-m-d',$lastyearE);

Answer:

Suppose if your current month is February or the month which has 30 days

echo date('Y-12-t', strtotime(date('Y-m-d'))); // if current month is february (2015-02-01) than it gives 2015-02-28

will give you inaccurate results

Solution:

So to get accurate result for the end date of an year, try the code below

$start_date = date("Y-01-01", strtotime("-1 year"));// get start date from here
$end_date = date("Y-12-t", strtotime($start_date));

(OR)

$last_year_last_month_date = date("Y-12-01", strtotime("-1 year"));
$end_date = date("Y-12-t", strtotime($last_year_last_month_date));