简体   繁体   English

在 PHP 中只比较没有年份的日期和月份,例如出生日期

[英]Compare only day and month without year in PHP e.g. birth date

I want to compare current date's day and month with subscription date's day and month only.我只想将当前日期的日期和月份与订阅日期的日期和月份进行比较。 For example:例如:

current date(d-m) = 3-6

And I want compare it with any other dm我想将它与任何其他dm进行比较
How should I do it in PHP In my project condition is like birth date in which we don't compare year.我应该如何在 PHP 中做到这一点在我的项目中,条件就像我们不比较年份的出生日期。

The trick in this is to let the month come first.诀窍是让月份先到。 This way PHP can compare the numbers by highest value.这样 PHP 可以按最高值比较数字。 Take a look at the following example:看看下面的例子:

$aDate = DateTime::createFromFormat('m-d', '05-20');
$bDate = DateTime::createFromFormat('m-d', '06-29');

if ($aDate->format('md') > $bDate->format('md')) {
    echo "'aDate' is bigger than 'bDate'";
}

use like使用喜欢

$current_date = date("d-m");
$subscription = "03-06-2016";
$subscription_date  = date("d-m", strtotime($subscription));
if($current_date ==$subscription_date)
{
    echo "date is equal";
}else
{
    echo "date is not equal";
}

This may help you这可能会帮助你

$sdate = $row['subscription_date'];
$date1 = date("m-d");
$date2 = date("m-d",strtotime($sdate)) ;
if ($date1 == $date2) {

}

Use DATE_FORMAT() function to extract part of date:使用 DATE_FORMAT() 函数提取部分日期:

Ref: http://dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-format参考: http : //dev.mysql.com/doc/refman/5.7/en/date-and-time-functions.html#function_date-format

SELECT * from table_name WHERE DATE_FORMAT(subscription_date, '%d-%m') = "05-05";

Use DateTime() PHP objects.使用 DateTime() PHP 对象。

Considering you have an array with user info from mysql query result: ( $userData['suscriptionDate'] )考虑到您有一个包含来自 mysql 查询结果的用户信息的数组:( $userData['suscriptionDate']

$today = new DateTime();

$userSuscription = new DateTime($userData['suscriptionDate']);

if ( $today->format('d') == $userSuscription->format('d') && $today->format('m') == $userSuscription->format('m')) {

    echo 'Congratulations!!';
}

I think, more elegant way to compare, especially when you have a full date with time is diff function of Datetime class:我认为,更优雅的比较方式,尤其是当你有一个完整的日期与时间是Datetime类的diff函数:

$d1 = new Datetime();
$d2 = new Datetime('+3 months +2 days +3 hours');

$diff = $d1->diff($d2);
var_dump($diff->d); // 2
var_dump($diff->m); // 2
// or have a comparison as a string
var_dump($diff->format('Difference is in %R%a days'));
// output: Difference is in 63 days

Enjoy!享受! Link to doc链接到文档

If you only need to check if the jn date is the same as the current date, then you don't need to make more than one function call.如果您只需要检查jn日期是否与当前日期相同,那么您不需要进行多次函数调用。 Because you are not comparing greater than or less than, the format of your input is unimportant.因为您不是在比较大于或小于,所以输入的格式并不重要。

Code: ( Demo )代码:(演示

$subscription = '29-11';
var_export(date("j-n") === $subscription);
// at the moment, the result is true
  • j is today's day of the month without any leading zeros and j是一个月中的今天没有任何前导零和
  • n is today's month without any leading zeros. n是今天的月份,没有任何前导零。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM