searls03 Posted January 1, 2013 Share Posted January 1, 2013 is there anyway to compare dates in the format of dd/mm/yyyy? if so, how do I do it? Do i need to convert to a different format? please help Quote Link to comment Share on other sites More sharing options...
searls03 Posted January 1, 2013 Author Share Posted January 1, 2013 mm/dd/yyyy format, sorry Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 1, 2013 Share Posted January 1, 2013 Yes. Quote Link to comment Share on other sites More sharing options...
searls03 Posted January 1, 2013 Author Share Posted January 1, 2013 ok.....so could you help me get started? Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 1, 2013 Share Posted January 1, 2013 You ask a vague question, you get a vague answer. Quote Link to comment Share on other sites More sharing options...
searls03 Posted January 1, 2013 Author Share Posted January 1, 2013 I don't know how to describe it well. I want to compare two dates to each other....two in mm/dd/yyyy format. I want to see if date1 is greater that date2. How can I go about with this. Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 1, 2013 Share Posted January 1, 2013 You'll want to use the DateTime library. http://us3.php.net/manual/en/book.datetime.php But mm/dd/yyyy is not a valid format to convert. Where are the dates coming from? Quote Link to comment Share on other sites More sharing options...
searls03 Posted January 1, 2013 Author Share Posted January 1, 2013 I am forming the dates using jquery's datepicker. it shows up as that format and then stores it in a database. I then have a calendar that pulls the date from the database and places it in the calendar. I can't use another format for this, as it is how the calendar is designed to work....believe me, I have tried other formats. I will take a look at the link you provided. I guess maybe I could insert the date into two columns or something and do it that way maybe. Quote Link to comment Share on other sites More sharing options...
searls03 Posted January 1, 2013 Author Share Posted January 1, 2013 what format do I need to put it in in order to compare, and how do I go about comparing? I think I found a code that may be able to convert to different formats, but I need to know what format it needs to be in. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 1, 2013 Share Posted January 1, 2013 That's not the right way to store dates in a database. In fact it's a pretty bad way to do it since you lose the ability to run comparisons on them, as you've undoubtedly noticed. Dates should be stored in YYYY-MM-DD format. Quote Link to comment Share on other sites More sharing options...
silkfire Posted January 1, 2013 Share Posted January 1, 2013 Comparing dates is a rather simple task in PHP. In your case I would suggest something like this: $date1 = strtotime($date1); $date2 = strtotime($date2) if ($date2 > $date1) { // CODE } Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 1, 2013 Share Posted January 1, 2013 As was said before, the format hes using is Not a valid format for PHP functions. Quote Link to comment Share on other sites More sharing options...
Pikachu2000 Posted January 1, 2013 Share Posted January 1, 2013 Not to mention that comparing dates in php that are stored in a database is about as efficient as a steam powered light bulb. Quote Link to comment Share on other sites More sharing options...
silkfire Posted January 1, 2013 Share Posted January 1, 2013 I just tried my code with an example date '08/01/2012' and it worked perfectly, Jessica Quote Link to comment Share on other sites More sharing options...
Barand Posted January 2, 2013 Share Posted January 2, 2013 Some formats work better than others <?php echo '<b>UK</b><br />'; echo '25/12/2012 --> ' . date('Y-m-d', strtotime('25/12/2012')) . '<br />'; echo '25-12-2012 --> ' . date('Y-m-d', strtotime('25-12-2012')) . '<br />'; echo '25.12.2012 --> ' . date('Y-m-d', strtotime('25.12.2012')) . '<br />'; echo '<b>US</b><br />'; echo '12/25/2012 --> ' . date('Y-m-d', strtotime('12/25/2012')) . '<br />'; echo '12-25-2012 --> ' . date('Y-m-d', strtotime('12-25-2012')) . '<br />'; echo '12.25.2012 --> ' . date('Y-m-d', strtotime('12.25.2012')) . '<br />'; ?> RESULTS: UK 25/12/2012 --> 1970-01-01 25-12-2012 --> 2012-12-25 ok 25.12.2012 --> 2012-12-25 ok US 12/25/2012 --> 2012-12-25 ok 12-25-2012 --> 1970-01-01 12.25.2012 --> 1970-01-01 Quote Link to comment Share on other sites More sharing options...
salathe Posted January 2, 2013 Share Posted January 2, 2013 But mm/dd/yyyy is not a valid format to convert. Where are the dates coming from? Sure it is, see Date Formats and in particular the "American month, day and year" format. Quote Link to comment Share on other sites More sharing options...
Jessica Posted January 2, 2013 Share Posted January 2, 2013 Yep, I stand corrected, when using the slashes only it works. Sorry about that. I do think it'd be better to fix the JS to use a YYYY-MM-DD format but Op should be able to get it working with strtotime now. Quote Link to comment Share on other sites More sharing options...
salathe Posted January 2, 2013 Share Posted January 2, 2013 I don't know which datepicker the OP is using, but any worth their salt would have a customisable output-date format. I'm with Jessica, in suggesting looking in that direction. Then you can compare the dates either using PHP's DateTime objects or with plain-old-string-comparison. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.