How to compare date in php

how to compare date in php

PHP program to compare two dates

Feb 11,  · Comparing two dates in PHP is simple when both the dates are in the same format but the problem arises when both dates are in a different format. Method 1: If the given dates are in the same format then use a simple comparison operator to compare the loveescorten.comted Reading Time: 40 secs. This works because of PHP's string comparison logic. Simply you can check if ($startdate if ($startdate > $date) {// do something} Both dates must be in the same format. Digits need to be zero-padded to the left and ordered from most significant to least significant. Y-m-d and Y-m-d H:i:s satisfy these conditions.

Join Stack Overflow to learn, share knowledge, and build your career. Connect and share knowledge within a single location that is structured and easy to search. If I wanted to compare today's date against the date in the database to see which one is greater, how would I do it? Update: Or simple use old-school date function:. I would'nt do this with PHP. A database should know, what day is today.

You can convert the dates into UNIX timestamps and compare the difference between them in seconds. Here's my spin on how to get the difference in days between two dates ;hp PHP. Now the use of '! Both dates must be in the same format. Digits need to be zero-padded to the left and ordered from most significant to least significant. Y-m-d and Y-m-d H:i:s satisfy compwre conditions. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Learn more. How can I compare two dates in PHP? Ask Question. Asked 10 years, 6 months ago.

Active 10 months ago. Viewed k times. The date is stored in the database in the following format If I wanted to compare today's date against the date in the database to see which one is greater, how would I do it? What's another way of doing it?

Improve this question. Dharman How to compare date in php Exploit 5, 16 16 gold badges compxre 63 silver badges 93 93 bronze badges. Assign the db dates to a DateTime object and then compare those objects. You can find a nice example in stackoverflow.

Add a comment. Active Oldest Pbp. Improve this answer. Matthew Matthew Davo, the comparison stops pgp the first character that is different. I'm sorry :was not careful enough. Hugo Peixoto Hugo Peixoto 3, 2 2 gold badges 16 16 silver badges 12 xompare bronze badges.

If What is in bearnaise sauce recall correctly, the concern about 1st of January of only applies to old versions of PHP running on Windows and it was never an issue on Unix.

I like this answer. Wouldn't it now be "If all your dates are posterior to the 1st of January of " and "previous to "? Note that the DateTime method is the preferred method. That object can do far more than strtotime ever could. Molle Dr. Molle k 14 14 gold badges silver badges bronze badges.

But you store expireDate in a db. Compare this date with NOW — Dr. Molle Oct 3 '10 at Atif Mahmood Atif Mahmood 8, 2 2 gold badges 39 39 silver badges 43 43 bronze badges. Here's a way on how to get the difference between two dates in minutes. Syno Syno 7 7 silver badges 22 22 bronze badges. The question compafe asking for comparing 2 dates - your answer is adding a load of extra stuff i. The part of your answer where the comparison takes places is virtually the same as the already existing answer here.

I've removed my downvote because of inn update, looks much better now : — crazyloonybin Dec 6 '17 at Shruthi reddy Shruthi reddy 31 1 how to paint plastic tumblers bronze badge. PiotrK PiotrK 89 1 1 silver badge 9 9 bronze badges.

Why this technique works should be explained in the answer. Please answer the OP's posted question, not how you solved your own unique scenario. This answer is ho the originally posted question. Please use the sample data provided by the OP. It is not exactly what the guy is asking for help but I thought this way could help if one understands fompare principles. I understand the principles, but this code does not answer the question asked.

This page is already bloated with incorrect and unrelated answers -- this only confuses researchers and wastes their time. I am trying to look out for researchers. Well I think it's not that difficult, he could've used the DateTime class instead of the date function. The answer Cmpare put above is in somehow to give ideas to the researchers.

I am going to stop repeating myself and disengage ftom this discussion. The 2nd highest solution stackoverflow. I have absolutely no idea what your magical requestDate method does -- there is no mention of it anywhere. I would not use your answer, nor recommend that any researcher use your solution. I do not foresee myself changing my vote. Show 4 more comments.

Found the answer on a blog and it's as simple as: strtotime date "Y". This appears to be solving a different problem. Please only answer the question asked by too OP. This works because of PHP's string comparison logic. Simply you can check Y-m-d with leading zero like will work. Here is proof that your technique will fail compre correctly compare the OP's commpare against today's date: 3v4l. The OP has a different date format.

This answer makes no attempt dage solve the original question. The OP has no interest in hours, minutes, or seconds. The Overflow Blog. How often do people actually copy and paste from Stack Overflow? Dompare we know. Podcast One in four visitors to Stack Overflow copies code. Featured on Meta. Stack Overflow for Teams is how to read regular playing cards as tarot free for up to 50 users, forever.

New onboarding comparee review queues. Should the [complete] tag be removed? Downvotes Survey results. Outdated Answers: results from use-case survey. Linked See more linked questions. Related Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled.

Accept all cookies Customize settings.

Your Answer

To compare two dates in PHP, the code is as follows. Here, we have used the equality operator to compare dates ?. Since this is the top result for "comparing dates in php". Pretty simple to use Datetime Objects (php >= ) and Compare them directly. $date1 = new DateTime (""); $date2 = new DateTime ("tomorrow"); // Can use date/string just like strtotime. var_dump ($date1. To compare two dates in PHP, the code is as follows ?Example Live Demophp $date_1 = new DateTime(); $date_2 = new DateTime(); if ($ Home Jobs.

Join Stack Overflow to learn, share knowledge, and build your career. Connect and share knowledge within a single location that is structured and easy to search. I want to compare two dates and time values in PHP.

One date is coming from MySQL, and second one is the current date. I want to run some code when both dates are the same. I tried the code below, but condition satisfies any time which is wrong. What is wrong with the code? I also tried to covert both dates with strtotime before comparing, but it gave me the same issue. The above condition satisfies any time even if both dates are different. Try this. On my server is working just great I've got something else because they aren't equal.

Date which I receive from database is type datetime format Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Learn more. Asked 4 years, 6 months ago. Active 4 years, 5 months ago. Viewed 5k times. Improve this question. Ruslan Osmanov Ketan Lathiya Ketan Lathiya 1 1 gold badge 8 8 silver badges 21 21 bronze badges.

How precisely do you want to compare them? Up to hours? No, its not duplicate question, I read that question but did not get answer what I want, that is why I asked new one. I want to compare date and time hours and minutes. I don't want to compare seconds. Add a comment. Active Oldest Votes.

Improve this answer. Kausha Thakkar Kausha Thakkar 10 10 bronze badges. What you have changed?. Explain properly. AHJeebon : i did convert the dates in to strtotime format because that's the right way to compare the date values. KaushaThakkar : Yes, this code runs fine for me. Thanks — Ketan Lathiya Oct 17 '16 at KetanLathiya Thanks!! Ruslan Osmanov Ruslan Osmanov Sign up or log in Sign up using Google.

Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. How often do people actually copy and paste from Stack Overflow? Now we know. Podcast One in four visitors to Stack Overflow copies code. Featured on Meta. Stack Overflow for Teams is now free for up to 50 users, forever.

New onboarding for review queues. Should the [complete] tag be removed? Downvotes Survey results. Outdated Answers: results from use-case survey. Linked 0. Related Hot Network Questions. Question feed. Stack Overflow works best with JavaScript enabled. Accept all cookies Customize settings.

5 thoughts on“How to compare date in php

Add a comment

Your email will not be published. Required fields are marked*