How to Compare Two Dates in PHP

How to compare two dates in php

You will have to make sure that your dates are valid date objects.

Try this:

$date1=date('d/m/y');
$tempArr=explode('_', '31_12_11');
$date2 = date("d/m/y", mktime(0, 0, 0, $tempArr[1], $tempArr[0], $tempArr[2]));

You can then perform the strtotime() method to get the difference.

How can I compare two dates in PHP?

in the database the date looks like this 2011-10-2

Store it in YYYY-MM-DD and then string comparison will work because '1' > '0', etc.

How to compare two dates from MYSQL using PHP

Just let de database handle it for you:

$sql = "SELECT name, date1, date2, IF(date1 < date2, date1, date2) FROM users";

Now $row[3] always contains the smallest date.

[Edit]

As OP only later mentioned the dates could also have a NULL value, the query needs adjusting. It now always returns the other date if one of the dates is empty, or NULL if both dates are empty.

SELECT id, date1, date2, 
// test if one of the dates (or both) is NULL
// if so return the one date, or NULL if both are empty
IF(date1 IS NULL OR date2 IS NULL, COALESCE(date1, date2),
// else compare dates
IF(date1 < date2, date1, date2)
) FROM test

Comparing two dates in PHP and MYSQL

Try this :

$current_datetime = date('Y-m-d H:i');
$send_date = date("Y-m-d H:i", strtotime($row['send_date'])); // suppose $row['send_date']'s value is '2016-10-17 15:00'
if(strtotime($current_datetime) == strtotime($send_date)){
//I want to run some code here
}else{
}

Hope it helps !!!!

How do I compare two DateTime objects in PHP 5.2.8?

The following seems to confirm that there are comparison operators for the DateTime class:

dev:~# php
<?php
date_default_timezone_set('Europe/London');

$d1 = new DateTime('2008-08-03 14:52:10');
$d2 = new DateTime('2008-01-03 11:11:10');
var_dump($d1 == $d2);
var_dump($d1 > $d2);
var_dump($d1 < $d2);
?>
bool(false)
bool(true)
bool(false)
dev:~# php -v
PHP 5.2.6-1+lenny3 with Suhosin-Patch 0.9.6.2 (cli) (built: Apr 26 2009 20:09:03)
Copyright (c) 1997-2008 The PHP Group
Zend Engine v2.2.0, Copyright (c) 1998-2008 Zend Technologies
dev:~#

What's the right way to compare two dates in php?

I use this to handle my database dates

    //Get the database date, and format it

$database_date = date_format($database_date, 'Y-m-d');

//Get today date or another date of you choice
$today_date = new DateTime();

//Format it
$today_date = date_format($today_date, 'Y-m-d');

// Use strtotime() to compare the dates or DateTime::diff for more cleaner IMO
$difference = strtotime($today_date) - strtotime($database_date);

Comparing two dates in php not working

$dateRegister = strtotime($register['date']);
if($dateRegister < strtotime(date('d/m/Y'))){
$class = "danger";
}else{
$class = "";
}

I am sure it will be helpful because strtotime returns timestamp and it executes exact comparing operators

Otherwise you should use Date Class and it's easy to get comparison of 2 dates.

$today = new DateTime('');
$expireDate = new DateTime($row->expireDate); //from database

if($today->format("Y-m-d") < $expireDate->format("Y-m-d")) {
//do something;
}

How to Compare Two Date with Time in Php

 $date1 = "2018-02-06 15:09:44";
$date2 = "2018-02-06 16:09:44";

if(strtotime($date1) < strtotime($date2)) {
echo "date1 less than date2";
} else {
echo "date1 is greater than date2;
}

compare two different dates in php

Just put your $monthToCheck into strtotime():

$monthToCheck  = strtotime(date('Y-m-01'));

Example



Related Topics



Leave a reply



Submit