Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date minus 1 year?

Tags:

date

php

I've got a date in this format:

2009-01-01 

How do I return the same date but 1 year earlier?

like image 982
ajsie Avatar asked Jan 02 '10 01:01

ajsie


People also ask

How to minus 3 years in Excel?

Add years to or subtract years from a date In cell A6, type =DATE(YEAR(A2)+B2,MONTH(A2),DAY(A2)), and then press RETURN . This formula adds the value in cell B2 (3 years) to the value in cell A2, for a result of 6/9/2012.

How do I subtract years from a date in python?

The easiest way to subtract years from a date in Python is to use the dateutil extension. The relativedelta object from the dateutil. relativedelta module allows you to subtract any number of years from a date object.

How do you subtract years in JavaScript?

function subtractYears(numOfYears, date = new Date()) { date. setFullYear(date. getFullYear() - numOfYears); return date; } // 👇️ subtract 1 year from current Date const result = subtractYears(1); // 👇️ Subtract 2 years from another Date const date = new Date('2022-04-26'); // 👇️ Sun Apr 26 2020 console.


1 Answers

You can use strtotime:

$date = strtotime('2010-01-01 -1 year'); 

The strtotime function returns a unix timestamp, to get a formatted string you can use date:

echo date('Y-m-d', $date); // echoes '2009-01-01' 
like image 67
Christian C. Salvadó Avatar answered Nov 13 '22 21:11

Christian C. Salvadó