计算出今天是 PHP 给定日期之后的 x 天

发布于 2024-12-04 04:20:03 字数 421 浏览 1 评论 0原文

我的数据库中有返回事件月/日/年数据点的数据。

我想做的是检查今天是否是我得到的月/日/年之后的 20 天。

到目前为止,我有这样的问题:

// Get date data
$day = $row['DAYOFMONTH(hike_date)'];
$year = $row['YEAR(hike_date)'];
$month = $row['MONTH(hike_date)'];

// Get today's date
$todays_date = date("Y-m-d");

// Create the date I am comparing with 
$date_string = $year.'-'.$month.'-'.$day;

我的问题是如何比较它?比较两个日期时,格式重要吗?

谢谢!

I have data in my database that returns the month/day/year data points for events.

What I want to do is check whether today is 20 days after the month/day/year that I get.

So far I have something like:

// Get date data
$day = $row['DAYOFMONTH(hike_date)'];
$year = $row['YEAR(hike_date)'];
$month = $row['MONTH(hike_date)'];

// Get today's date
$todays_date = date("Y-m-d");

// Create the date I am comparing with 
$date_string = $year.'-'.$month.'-'.$day;

My question is how do I compare it? And is the format going to matter in comparing the two dates?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

平生欢 2024-12-11 04:20:03

将月/日/年解析为 DateTime:

$other = date_create($year.'-'.$month.'-'.$day);

添加 20 天:

$twenty_days_after = clone $other;
$twenty_days_after->modify('+20 days');

与今天比较:

if ($today >= $twenty_days_after) {
    // today is 20 days after the month/day/year that you got
}

参见 date_create

Parse the month/day/year into a DateTime:

$other = date_create($year.'-'.$month.'-'.$day);

Add 20 days to it:

$twenty_days_after = clone $other;
$twenty_days_after->modify('+20 days');

Compare it with today:

if ($today >= $twenty_days_after) {
    // today is 20 days after the month/day/year that you got
}

See date_create.

巴黎盛开的樱花 2024-12-11 04:20:03
$database_date             = strtotime(sprintf('%s-%s-%s', $year,$month,$day));
$twenty_days_from_now      = strtotime('+20 days');
$twenty_days_From_database = strtotime(sprintf('%s-%s-%s +20 days', $year,$month,$day));

使用 strttime 是最简单的方法。

$database_date             = strtotime(sprintf('%s-%s-%s', $year,$month,$day));
$twenty_days_from_now      = strtotime('+20 days');
$twenty_days_From_database = strtotime(sprintf('%s-%s-%s +20 days', $year,$month,$day));

Using strttime is the easiest method.

一枫情书 2024-12-11 04:20:03

使用

strtotime()

函数将您的日期转换为时间戳,您可以更轻松地比较它们

use

strtotime()

function which will convert your dates into timestamp and you can compare them easier

避讳 2024-12-11 04:20:03
// Get date data
$day = $row['DAYOFMONTH(hike_date)'];
$year = $row['YEAR(hike_date)'];
$month = $row['MONTH(hike_date)'];

// Get today's date
$todays_date = date("Y-m-d");

// Create the date I am comparing with 
$date_string = $year.'-'.$month.'-'.$day;

if ( date( 'Y-m-d', strtotime( "+20 days", $date_string ) ) {
    //date is 20 days before today
}

strtotime 做了神奇的事情

// Get date data
$day = $row['DAYOFMONTH(hike_date)'];
$year = $row['YEAR(hike_date)'];
$month = $row['MONTH(hike_date)'];

// Get today's date
$todays_date = date("Y-m-d");

// Create the date I am comparing with 
$date_string = $year.'-'.$month.'-'.$day;

if ( date( 'Y-m-d', strtotime( "+20 days", $date_string ) ) {
    //date is 20 days before today
}

strtotime does magical things

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文