PHP check if two Datetimes are not on the same calendar day
Just create the dates with time set to 00:00:00:
$startTime = \DateTime::createFromFormat('Y/m/d H:i:s', '2015/01/01 00:00:00');
$endTime = \DateTime::createFromFormat('Y/m/d H:i:s', '2015/01/02 00:00:00');
or reset time to zero on existing dates:$startTime->setTime(0, 0, 0);
$endTime->setTime(0, 0, 0);
then it should work:$diff = $startTime->diff($endTime);
$days = $diff->format('%d');
echo $days; // 1
BonusIf you want to work only with dates, remember to set the time to 00:00:00 in createFromFormat
or reset it with setTime
. If you won't provide time in createFromFormat
PHP will set it to the current time:
$date = DateTime::createFromFormat('Y-m-d', '2016-01-21');
print $date->format('H:i:s'); //not 00:00:00
To fix it, you must either:provide 00:00:00 time in format:
$date = DateTime::createFromFormat('Y-m-d H:i:s', '2016-01-21 00:00:00');
prefix the date format with exclamation mark and omit the time, this will set the time to 00:00:00 automatically:
$date = DateTime::createFromFormat('!Y-m-d', '2016-01-21');
reset the time after creation:
$date = DateTime::createFromFormat('Y-m-d', '2016-01-21');
$date->setTime(0, 0);
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:~#
How can I compare two dates in PHP?
Store it in YYYY-MM-DD and then string comparison will work because '1' > '0', etc.in the database the date looks like this 2011-10-2
PHP - comparing datetime objects with different timezone
DateTime objects with different time zones are considered the same for simple comparison if they represent the same time.
Example:
$dt1 = new DateTime('2019-10-10 00:00', new DateTimeZone('UTC'));
$dt2 = new DateTime('2019-10-10 03:00', new DateTimeZone('Europe/Bucharest'));
var_dump($dt1 == $dt2); //bool(true)
The result of comparison is equal because at the time of 2019-10-10 00:00 it was already 03:00 in Bucharest.The comparisons with larger and smaller work analogously.
Note: DateTime has implemented special comparisons for this and reacts differently than the comparison of "normal objects"
PHP date time greater than today
You are not comparing dates. You are comparing strings. In the world of string comparisons, 09/17/2015
> 01/02/2016
because 09
> 01
. You need to either put your date in a comparable string format or compare DateTime
objects which are comparable.
<?php
$date_now = date("Y-m-d"); // this format is string comparable
if ($date_now > '2016-01-02') {
echo 'greater than';
}else{
echo 'Less than';
}
DemoOr
<?php
$date_now = new DateTime();
$date2 = new DateTime("01/02/2016");
if ($date_now > $date2) {
echo 'greater than';
}else{
echo 'Less than';
}
Demo Comparing DateTime date values in PHP
You could use array_filter
:
<?php
$dates = array(
new DateTime('2000-12-01'),
new DateTime('2005-01-20'),
new DateTime('2010-05-06'),
new DateTime('2008-04-03'),
new DateTime('2007-11-05'),
);
$theDate = new DateTime('2010-05-06');
$result = array_shift(array_filter($dates, function($d) use ($theDate) {
return $d->format('Y-m-d') == $theDate->format('Y-m-d');
}));
var_dump($result);
$result
is either a DateTime
object (if found) or otherwise false
.
Related Topics
PHP Script to Log the Raw Data of Post
Notice: Ob_End_Flush(): Failed to Send Buffer of Zlib Output Compression (1) In
Cyrillic Transliteration in PHP
How to Force Ssl in Codeigniter
PHP MySQL Group by to Get Latest Record, Not First Record
How to Get Content of Remote HTML Page
Codeigniter 4 Problem Installing with Composer
Running PHP 5.4 Built-In Web Server Outside Localhost
0' as a String with Empty() in PHP
How to Select Year and Month from the Created_At Attributes of Database Table in Laravel 5.1
Php: $_Server Variables: $_Server['Http_Host'] VS $_Server['Server_Name']
Symfony 2.7 Cache:Clear Command Checks Every Database Connection
Zend Framework 2 - Global Check for Authentication with Zfcuser
Find Windows 32 or 64 Bit Using PHP
Str_Replace() for Multiple Value Replacement