How to get the start and end date last year?

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

+4
source share
5 answers

The first day is always January 1, the last day is always December 31. You really change the year attached to it. Depending on how you want to format the date, you have a couple of possibilities ...

  • 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}"; 
  • If you need a timestamp for both of these 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 want. The package is the same.

+6
source

You can do this using the below. Hope this helps someone.

 //to get start date of previous year echo date("dmy",strtotime("last year January 1st")); //to get end date of previous year echo date("dmy",strtotime("last year December 31st")); 
+1
source

year start date:

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

year end date:

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

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('Ym-d',$lastyearS)."<br/>";echo date('Ym-d',$lastyearE); 
0
source

Suppose your current month is February or a month that has 30 days

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

will give you inaccurate results

Decision:

So, to get the exact result for the end date of the 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)); 
0
source

Source: https://habr.com/ru/post/1401952/


All Articles