3
votes

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 d-m
How should I do it in PHP In my project condition is like birth date in which we don't compare year.

6
Convert your date to such format :date("d-M", strtotime("<your_Date_here"))Thamilhan

6 Answers

6
votes

The trick in this is to let the month come first. This way PHP can compare the numbers by highest value. 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'";
}
2
votes

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";
}
1
votes

This may help you

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

}
0
votes

Use DateTime() PHP objects.

Considering you have an array with user info from mysql query result: ($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!!';
}
0
votes

Use DATE_FORMAT() function to extract part of date:

Ref: 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";
0
votes

I think, more elegant way to compare, especially when you have a full date with time is diff function of Datetime class:

$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