Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subtracting days, months or years from date using php [closed]

Tags:

I have a db table with a date field startBefore in ('Y-m-d') format. There are also 3 other int fields representing years, months and days. What I would like to do is to fill these three fields the new startAfter date in 'Y-m-d' format.

For example I have the '2001-11-14' as startBefore date and I would like to subtract 3 yrs, 7 months, 1 day to get the startAfter date.

Any ideas how I can accomplish this?

Thanks!

like image 292
dimoss Avatar asked Jan 03 '14 10:01

dimoss


People also ask

Can I subtract dates in PHP?

The date_sub() function subtracts some days, months, years, hours, minutes, and seconds from a date.

How can I get the difference between two dates and time in PHP?

You can use strtotime() function to get the time difference between two dates (DateTimes) in minutes using PHP.

What does date () do in PHP?

PHP date() Function The PHP date function is used to format a date or time into a human readable format. It can be used to display the date of article was published. record the last updated a data in a database.


1 Answers

use strtotime('date -years -months -days')

  <?php     $time = strtotime('2001-11-14 -3 years -7 months -5 days');     echo $date = date("Y-m-d", $time); 
like image 148
Nambi Avatar answered Sep 21 '22 14:09

Nambi