用php减去日期、月份或年份。

时间:2022-08-25 18:13:30

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.

我有一个db表格,上面有一个日期字段startBefore ('Y-m-d')格式。还有另外3个int字段表示年份、月份和日期。我想做的是用“Y-m-d”格式填充这三个字段。

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.

例如,我有“2001-11-14”作为开始日期,我想减去3年,7个月,1天的开始日期。

Any ideas how I can accomplish this?

你知道我该怎么做吗?

Thanks!

谢谢!

4 个解决方案

#1


19  

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

使用strtotime(“日期-年-月-日”)

  <?php
    $time = strtotime('2001-11-14 -3 years -7 months -5 days');
    echo $date = date("Y-m-d", $time);

#2


9  

Here comes the DateTime:

DateTime:来了

$start_date = '2013-03-06';
$date = DateTime::createFromFormat('Y-m-d',$start_date);

$date->modify('+1 month');
echo $date->format('Y-m-d');//2013-04-06

$date->modify('+4 year');
echo $date->format('Y-m-d');//2017-04-06

$date->modify('+6 day');
echo $date->format('Y-m-d');//2017-04-12

$date->modify('+24 hours');
echo $date->format('Y-m-d');//2017-04-13

$date->modify('-7 years');
echo $date->format('Y-m-d'); //2010-04-13

$date->modify('-18 months');
echo $date->format('Y-m-d'); //2008-10-13

So on and so forth.

等等。

#3


2  

You Could try

你可以试试

strtotime function in php its quite simple

strtotime函数在php中非常简单

<?php 
echo date("jS F, Y", strtotime("11.12.10")); 
// outputs 10th December, 2011 

echo date("jS F, Y", strtotime("11/12/10")); 
// outputs 12th November, 2010 

echo date("jS F, Y", strtotime("11-12-10")); 
// outputs 11th December, 2010  
?> 

#4


0  

One way is to make the startBefore date a timestamp, than working with numbers and re-obtain the startAfter date, another way is to try to subtract days, months and years directly from the date.

一种方法是将开始日期设置为时间戳,而不是使用数字并重新获得开始日期之后的日期,另一种方法是尝试直接从日期减去天数、月数和年数。

#1


19  

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

使用strtotime(“日期-年-月-日”)

  <?php
    $time = strtotime('2001-11-14 -3 years -7 months -5 days');
    echo $date = date("Y-m-d", $time);

#2


9  

Here comes the DateTime:

DateTime:来了

$start_date = '2013-03-06';
$date = DateTime::createFromFormat('Y-m-d',$start_date);

$date->modify('+1 month');
echo $date->format('Y-m-d');//2013-04-06

$date->modify('+4 year');
echo $date->format('Y-m-d');//2017-04-06

$date->modify('+6 day');
echo $date->format('Y-m-d');//2017-04-12

$date->modify('+24 hours');
echo $date->format('Y-m-d');//2017-04-13

$date->modify('-7 years');
echo $date->format('Y-m-d'); //2010-04-13

$date->modify('-18 months');
echo $date->format('Y-m-d'); //2008-10-13

So on and so forth.

等等。

#3


2  

You Could try

你可以试试

strtotime function in php its quite simple

strtotime函数在php中非常简单

<?php 
echo date("jS F, Y", strtotime("11.12.10")); 
// outputs 10th December, 2011 

echo date("jS F, Y", strtotime("11/12/10")); 
// outputs 12th November, 2010 

echo date("jS F, Y", strtotime("11-12-10")); 
// outputs 11th December, 2010  
?> 

#4


0  

One way is to make the startBefore date a timestamp, than working with numbers and re-obtain the startAfter date, another way is to try to subtract days, months and years directly from the date.

一种方法是将开始日期设置为时间戳,而不是使用数字并重新获得开始日期之后的日期,另一种方法是尝试直接从日期减去天数、月数和年数。