COMPARING PHP OBJECTS (compound type) We have seen that PHP does a lot of type-juggling on its own - which can wreak havoc in unexpected ways - but it is still up to us to produce code that is clear, maintainable AND follows the rules we want to follow. When creating a PHP Object, it is sometimes unclear what makes two of them the same. Nov 10, 2016 Possible duplicate of PHP compare two dates – Ruslan Osmanov Nov 11 '16 at 12:13.
I need to compare the dates from my database with the current day. This id my code, in eloquent:
I want to retrieve today's posts only.Knowing that the 'date' field is of type Date, How do I make this work?I tried to convert
date('Y-m-d')
to string with the 'format' method, but it seems that date('Y-m-d')
is somehow returning a Boolean, and thus, the method 'format' can't be applied to it..ZakariaZakaria
2 Answers
konnectionkonnection
If i am getting what you want, you can make use of strtotime function. It converts datetime string passed to it to Unix timestamp which you can use for comparison.
If date from
2015-10-21
then the function will return 1445385600
and if today's date from date(Y-m-d)
is 2015-10-31 then the function will return 1446249600
so you can compare the two easily.Then comparison can be made as
if($var1 $var2)
SuyogSuyog